ShatteredSword/src/main.ts

128 lines
3.9 KiB
TypeScript
Raw Normal View History

import GameLoop from "./Loop/GameLoop";
2020-08-17 20:09:41 -04:00
import Player from "./Player";
import UIElement from "./Nodes/UIElement";
import Color from "./Utils/Color";
2020-08-10 16:44:24 -04:00
import Button from "./Nodes/UIElements/Button";
2020-08-11 19:55:05 -04:00
import {} from "./index";
2020-08-17 10:34:31 -04:00
import OrthogonalTilemap from "./Nodes/Tilemaps/OrthogonalTilemap";
2020-08-03 16:51:20 -04:00
function main(){
// Create the game object
let game = new GameLoop();
let gameState = game.getGameState();
let backgroundScene = gameState.createScene();
backgroundScene.setParallax(0.5, 0.8);
backgroundScene.setAlpha(0.5);
let mainScene = gameState.createScene();
let uiLayer = gameState.createScene();
uiLayer.setParallax(0, 0);
let pauseMenu = gameState.createScene();
pauseMenu.setParallax(0, 0);
// Initialize GameObjects
2020-08-17 10:34:31 -04:00
let recordButton = uiLayer.canvasNode.add(Button);
recordButton.setSize(100, 50);
recordButton.setText("Record");
recordButton.setPosition(400, 30);
recordButton.onClickEventId = "record_button_press";
2020-08-17 10:34:31 -04:00
let stopButton = uiLayer.canvasNode.add(Button);
stopButton.setSize(100, 50);
stopButton.setText("Stop");
stopButton.setPosition(550, 30);
stopButton.onClickEventId = "stop_button_press";
2020-08-17 10:34:31 -04:00
let playButton = uiLayer.canvasNode.add(Button);
playButton.setSize(100, 50);
playButton.setText("Play");
playButton.setPosition(700, 30);
playButton.onClickEventId = "play_button_press";
2020-08-17 10:34:31 -04:00
let cycleFramerateButton = uiLayer.canvasNode.add(Button);
cycleFramerateButton.setSize(150, 50);
cycleFramerateButton.setText("Cycle FPS");
cycleFramerateButton.setPosition(5, 400);
let i = 0;
let fps = [15, 30, 60];
cycleFramerateButton.onClick = () => {
game.setMaxFPS(fps[i]);
i = (i + 1) % 3;
}
2020-08-17 10:34:31 -04:00
let pauseButton = uiLayer.canvasNode.add(Button);
pauseButton.setSize(100, 50);
pauseButton.setText("Pause");
pauseButton.setPosition(700, 400);
pauseButton.onClick = () => {
mainScene.setPaused(true);
pauseMenu.enable();
}
2020-08-17 10:34:31 -04:00
let modalBackground = pauseMenu.canvasNode.add(UIElement);
modalBackground.setSize(400, 200);
modalBackground.setBackgroundColor(new Color(0, 0, 0, 0.4));
modalBackground.setPosition(200, 100);
2020-08-17 10:34:31 -04:00
let resumeButton = pauseMenu.canvasNode.add(Button);
resumeButton.setSize(100, 50);
resumeButton.setText("Resume");
resumeButton.setPosition(400, 200);
resumeButton.onClick = () => {
mainScene.setPaused(false);
pauseMenu.disable();
}
2020-08-20 20:45:48 -04:00
backgroundScene.tilemap.add(OrthogonalTilemap, "assets/tilemaps/Background.json");
mainScene.tilemap.add(OrthogonalTilemap, "assets/tilemaps/Platformer.json");
let player = mainScene.physics.add(Player, "platformer");
2020-08-20 20:45:48 -04:00
// mainScene.tilemap.add(OrthogonalTilemap, "assets/tilemaps/TopDown.json");
// let player = mainScene.physics.add(Player, "topdown");
2020-08-18 14:48:47 -04:00
mainScene.getViewport().follow(player);
pauseMenu.disable();
game.start();
2020-08-03 16:51:20 -04:00
}
2020-08-11 19:55:05 -04:00
CanvasRenderingContext2D.prototype.roundedRect = function(x: number, y: number, w: number, h: number, r: number): void {
// Clamp the radius between 0 and the min of the width or height
if(r < 0) r = 0;
if(r > Math.min(w, h)) r = Math.min(w, h);
// Draw the rounded rect
this.beginPath();
// Top
this.moveTo(x + r, y);
this.lineTo(x + w - r, y);
this.arcTo(x + w, y, x + w, y + r, r);
// Right
this.lineTo(x + w, y + h - r);
this.arcTo(x + w, y + h, x + w - r, y + h, r);
// Bottom
this.lineTo(x + r, y + h);
this.arcTo(x, y + h, x, y + h - r, r);
// Left
this.lineTo(x, y + r);
this.arcTo(x, y, x + r, y, r)
this.closePath();
}
CanvasRenderingContext2D.prototype.strokeRoundedRect = function(x, y, w, h, r){
this.roundedRect(x, y, w, h, r);
this.stroke();
}
CanvasRenderingContext2D.prototype.fillRoundedRect = function(x, y, w, h, r){
this.roundedRect(x, y, w, h, r);
this.fill();
}
main();