feat: added checkpoints generator and timer

This commit is contained in:
Renge 2022-04-23 20:45:17 -04:00
parent a05c0a1721
commit 797d571b76
4 changed files with 145 additions and 54 deletions

View File

@ -52,7 +52,7 @@
"possibility": 1 "possibility": 1
} }
], ],
"checkPoint": [5, 19, 5, 5] "startCheckPoint": [25, 19, 5, 5]
}, },
"exit": { "exit": {
"width": 30, "width": 30,
@ -76,7 +76,8 @@
"width": 3, "width": 3,
"alt_tile": [0, 478] "alt_tile": [0, 478]
} }
] ],
"endCheckPoint": [20, 16, 5, 5]
}, },
"rooms": [ "rooms": [
{ {

View File

@ -99,36 +99,44 @@ export default class GameLevel extends Scene {
protected gameStateStack: Stack<GameState>; protected gameStateStack: Stack<GameState>;
// Story // Story
private storytextLabel: Label; protected storytextLabel: Label;
private storyLayer: Layer; protected storyLayer: Layer;
private story: Story; protected story: Story;
private storyProgress: number; protected storyProgress: number;
private storySprites: Array<Sprite>; protected storySprites: Array<Sprite>;
private storyBGMs: Array<string>; protected storyBGMs: Array<string>;
private currentSpeaker: string; protected currentSpeaker: string;
private currentContent: string; protected currentContent: string;
//buffs layer //buffs layer
buffLayer: Layer; protected buffLayer: Layer;
buffButton1 : Button; protected buffButton1 : Button;
buffLabel1 : Label; protected buffLabel1 : Label;
buffButton2 : Button; protected buffButton2 : Button;
buffLabel2 : Label; protected buffLabel2 : Label;
buffButton3 : Button; protected buffButton3 : Button;
buffLabel3: Label; protected buffLabel3: Label;
buffs: Array<Buff>; protected buffs: Array<Buff>;
//pause layer //pause layer
pauseLayer: Layer; protected pauseLayer: Layer;
pauseText: Label; protected pauseText: Label;
pauseInput: TextInput; protected pauseInput: TextInput;
pauseSubmit: Label; protected pauseSubmit: Label;
pauseCheatText: Label; protected pauseCheatText: Label;
protected randomSeed: number; protected randomSeed: number;
protected rmg: RandomMapGenerator; protected rmg: RandomMapGenerator;
protected map: TiledTilemapData; protected map: TiledTilemapData;
protected startCheckPoint: Rect;
protected endCheckPoint: Rect;
protected touchedStartCheckPoint: boolean = false;
protected touchedEndCheckPoint: boolean = false;
protected static gameTimer: number = 0;
protected gameStarted: boolean = false;
protected timerLable: Label;
startpos: Vec2; startpos: Vec2;
loadScene(): void { loadScene(): void {
//can load player sprite here //can load player sprite here
@ -198,8 +206,10 @@ export default class GameLevel extends Scene {
this.subscribeToEvents(); this.subscribeToEvents();
this.addUI(); this.addUI();
const checkPoint = this.rmg.getCheckPoint(); let startCheckPoint = this.rmg.getStartCheckPoint();
this.addLevelEnd(new Vec2(checkPoint[0], checkPoint[1]), new Vec2(checkPoint[2], checkPoint[3])); this.startCheckPoint = this.addCheckPoint(new Vec2(startCheckPoint[0], startCheckPoint[1]), new Vec2(startCheckPoint[2], startCheckPoint[3]), "startStory", "startTimer");
let endCheckPoint = this.rmg.getEndCheckPoint();
this.endCheckPoint = this.addCheckPoint(new Vec2(endCheckPoint[0], endCheckPoint[1]), new Vec2(endCheckPoint[2], endCheckPoint[3]), "endStory", "nextLevel");
// Create an enemies array // Create an enemies array
// Send the player and enemies to the battle manager // Send the player and enemies to the battle manager
@ -250,7 +260,30 @@ export default class GameLevel extends Scene {
updateScene(deltaT: number){ updateScene(deltaT: number){
if (this.gameStateStack.peek() === GameState.GAMING) {
if (this.gameStarted) {
GameLevel.gameTimer += deltaT;
let minutes = Math.floor(GameLevel.gameTimer / 60);
if (minutes >= 10) {
this.timerLable.text = minutes.toString();
}
else {
this.timerLable.text = "0" + minutes.toString();
}
let seconds = Math.floor(GameLevel.gameTimer % 60);
if (seconds >= 10) {
this.timerLable.text += ":" + seconds.toString();
}
else {
this.timerLable.text += ":0" + seconds.toString();
}
this.timerLable.textColor = Color.BLACK;
}
else {
this.timerLable.textColor = Color.RED;
}
}
// Handle events and update the UI if needed // Handle events and update the UI if needed
while(this.receiver.hasNextEvent()){ while(this.receiver.hasNextEvent()){
let event = this.receiver.getNextEvent(); let event = this.receiver.getNextEvent();
@ -326,6 +359,18 @@ export default class GameLevel extends Scene {
this.sceneManager.changeToScene(GameOver, {}); this.sceneManager.changeToScene(GameOver, {});
} }
break; break;
case "startStory":
this.playStartStory();
break;
case "endStory":
this.playEndStory();
break;
case "startTimer":
this.startTimer();
break;
case "nextLevel":
this.goToNextLevel();
break;
} }
} }
@ -435,10 +480,10 @@ export default class GameLevel extends Scene {
} }
*/ */
if (InputWrapper.isInventoryJustPressed()) { // if (InputWrapper.isInventoryJustPressed()) {
console.log("LoadingStory"); // console.log("LoadingStory");
this.storyLoader("shattered_sword_assets/jsons/story.json"); // this.storyLoader("shattered_sword_assets/jsons/story.json");
} // }
@ -503,6 +548,10 @@ export default class GameLevel extends Scene {
this.receiver.subscribe("buff2"); this.receiver.subscribe("buff2");
this.receiver.subscribe("buff3"); this.receiver.subscribe("buff3");
this.receiver.subscribe("cheat"); this.receiver.subscribe("cheat");
this.receiver.subscribe("startStory");
this.receiver.subscribe("startTimer");
this.receiver.subscribe("endStory");
this.receiver.subscribe("nextLevel");
} }
// TODO - // TODO -
@ -693,9 +742,12 @@ export default class GameLevel extends Scene {
this.pauseSubmit.onClickEventId = "cheat"; this.pauseSubmit.onClickEventId = "cheat";
this.pauseSubmit.borderWidth = 3; this.pauseSubmit.borderWidth = 3;
this.livesCountLabel = <Label>this.add.uiElement(UIElementType.LABEL, "UI", {position: new Vec2(600, 30), text:"Lives: "}); this.livesCountLabel = <Label>this.add.uiElement(UIElementType.LABEL, "UI", {position: new Vec2(this.viewport.getHalfSize().x*2 - 100, 30), text:"Lives: "});
this.livesCountLabel.textColor = Color.YELLOW; this.livesCountLabel.textColor = Color.YELLOW;
this.livesCountLabel.fontSize = 25;
this.timerLable = <Label>this.add.uiElement(UIElementType.LABEL, "UI", {position: new Vec2(Math.floor(this.viewport.getHalfSize().x), 30), text: "00:00"});
this.timerLable.fontSize = 60;
} }
//TODO - determine whether we will have weapon datatype //TODO - determine whether we will have weapon datatype
@ -738,8 +790,6 @@ export default class GameLevel extends Scene {
* Initializes the player * Initializes the player
*/ */
protected initPlayer(): void { protected initPlayer(): void {
//create the inventory //create the inventory
let inventory = new InventoryManager(this, 1, "inventorySlot", new Vec2(16, 16), 4, "slots1", "items1"); let inventory = new InventoryManager(this, 1, "inventorySlot", new Vec2(16, 16), 4, "slots1", "items1");
@ -850,8 +900,6 @@ export default class GameLevel extends Scene {
//TODO - give each enemy unique weapon //TODO - give each enemy unique weapon
protected initializeEnemies( enemies: Enemy[]){ protected initializeEnemies( enemies: Enemy[]){
let actionsDefault = [new AttackAction(3, [Statuses.IN_RANGE], [Statuses.REACHED_GOAL]), let actionsDefault = [new AttackAction(3, [Statuses.IN_RANGE], [Statuses.REACHED_GOAL]),
new Move(2, [], [Statuses.IN_RANGE], {inRange: 60}), new Move(2, [], [Statuses.IN_RANGE], {inRange: 60}),
]; ];
@ -936,11 +984,12 @@ export default class GameLevel extends Scene {
} }
protected addLevelEnd(startingTile: Vec2, size: Vec2): void { protected addCheckPoint(startingTile: Vec2, size: Vec2, enter: string, exit: string): Rect {
this.levelEndArea = <Rect>this.add.graphic(GraphicType.RECT, "primary", {position: startingTile.scale(32), size: size.scale(32)}); let checkPoint = <Rect>this.add.graphic(GraphicType.RECT, "primary", {position: startingTile.scale(32), size: size.scale(32)});
this.levelEndArea.addPhysics(undefined, undefined, false, true); checkPoint.addPhysics(undefined, undefined, false, true);
// this.levelEndArea.setTrigger("player", somelevelendevent, null); checkPoint.setTrigger("player", enter, null);
this.levelEndArea.color = new Color(0, 0, 0, 0); checkPoint.color = new Color(0, 0, 0, 0);
return checkPoint;
} }
@ -1005,7 +1054,7 @@ export default class GameLevel extends Scene {
* @param viewportCenter The center of the viewport * @param viewportCenter The center of the viewport
* @param viewportSize The size of the viewport * @param viewportSize The size of the viewport
*/ */
playerFalloff(viewportCenter: Vec2, viewportSize: Vec2):void{ protected playerFalloff(viewportCenter: Vec2, viewportSize: Vec2):void{
if(this.player.position.y >= viewportCenter.y +viewportSize.y/2.0){ if(this.player.position.y >= viewportCenter.y +viewportSize.y/2.0){
this.player.position.set(this.playerSpawn.x,this.playerSpawn.y); this.player.position.set(this.playerSpawn.x,this.playerSpawn.y);
@ -1018,7 +1067,36 @@ export default class GameLevel extends Scene {
} }
async storyLoader(storyPath: string) { protected playStartStory() {
if (!this.touchedStartCheckPoint) {
this.touchedStartCheckPoint = true;
this.storyLoader("shattered_sword_assets/jsons/story.json");
this.startTimer();
}
}
protected playEndStory() {
if (!this.touchedEndCheckPoint) {
this.touchedEndCheckPoint = true;
this.storyLoader("shattered_sword_assets/jsons/story.json");
this.endTimer();
}
}
protected startTimer() {
this.gameStarted = true;
}
protected endTimer() {
this.gameStarted = false;
}
protected goToNextLevel() {
console.log("goToNextLevel")
}
protected async storyLoader(storyPath: string) {
if (this.gameStateStack.peek() === GameState.STORY) { if (this.gameStateStack.peek() === GameState.STORY) {
return; return;
} }
@ -1055,11 +1133,11 @@ export default class GameLevel extends Scene {
this.updateStory(); this.updateStory();
} }
hasNextStory(): boolean { protected hasNextStory(): boolean {
return this.gameStateStack.peek() === GameState.STORY && this.storyProgress + 1 < this.story.texts.length; return this.gameStateStack.peek() === GameState.STORY && this.storyProgress + 1 < this.story.texts.length;
} }
updateStory() { protected updateStory() {
if (this.hasNextStory()) { if (this.hasNextStory()) {
this.storyProgress++; this.storyProgress++;
let tmp = undefined; let tmp = undefined;
@ -1136,7 +1214,7 @@ export default class GameLevel extends Scene {
} }
// Cheat // Cheat
enableCheat() { protected enableCheat() {
if (this.pauseInput.text.toUpperCase() === "UUDDLRLRBABA") { if (this.pauseInput.text.toUpperCase() === "UUDDLRLRBABA") {
(<PlayerController>this.player._ai).godMode = true; (<PlayerController>this.player._ai).godMode = true;
} }

View File

@ -50,7 +50,8 @@ export class RoomTemplate {
topLayer: Array<number>; topLayer: Array<number>;
entrances: Array<Entrance>; entrances: Array<Entrance>;
sprites?: Array<Sprite>; sprites?: Array<Sprite>;
checkPoint?: [number, number, number, number]; startCheckPoint?: [number, number, number, number];
endCheckPoint?: [number, number, number, number];
} }
export class Corner { export class Corner {

View File

@ -25,7 +25,8 @@ export default class RandomMapGenerator {
private exitFacing: Facing; private exitFacing: Facing;
private enemies: Array<Enemy>; private enemies: Array<Enemy>;
private player: Vec2; private player: Vec2;
private checkPoint: [number, number, number, number]; private startCheckPoint: [number, number, number, number];
private endCheckPoint: [number, number, number, number];
constructor(JSONFilePath: string, seed: any) { constructor(JSONFilePath: string, seed: any) {
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
@ -45,7 +46,8 @@ export default class RandomMapGenerator {
this.rooms = new Array(); this.rooms = new Array();
this.enemies = new Array(); this.enemies = new Array();
this.player = new Vec2(); this.player = new Vec2();
this.checkPoint = [0,0,0,0]; this.startCheckPoint = [0,0,0,0];
this.endCheckPoint = [0, 0, 0, 0];
let gen = require('random-seed'); let gen = require('random-seed');
this.gen = new gen(seed); this.gen = new gen(seed);
this.hasExit = false; this.hasExit = false;
@ -117,8 +119,12 @@ export default class RandomMapGenerator {
return new Vec2(this.player.x - this.minX, this.player.y - this.minY); return new Vec2(this.player.x - this.minX, this.player.y - this.minY);
} }
getCheckPoint(): [number, number, number, number] { getStartCheckPoint(): [number, number, number, number] {
return [this.checkPoint[0] - this.minX, this.checkPoint[1] - this.minY, this.checkPoint[2], this.checkPoint[3]]; return [this.startCheckPoint[0] - this.minX, this.startCheckPoint[1] - this.minY, this.startCheckPoint[2], this.startCheckPoint[3]];
}
getEndCheckPoint(): [number, number, number, number] {
return [this.endCheckPoint[0] - this.minX, this.endCheckPoint[1] - this.minY, this.endCheckPoint[2], this.endCheckPoint[3]];
} }
getEnemies(): Array<Enemy> { getEnemies(): Array<Enemy> {
@ -378,10 +384,15 @@ export default class RandomMapGenerator {
} }
} }
} }
if (old.checkPoint) { if (old.startCheckPoint) {
this.checkPoint = [...old.checkPoint]; this.startCheckPoint = [...old.startCheckPoint];
this.checkPoint[0] += posX; this.startCheckPoint[0] += posX;
this.checkPoint[1] += posY; this.startCheckPoint[1] += posY;
}
if (old.endCheckPoint) {
this.endCheckPoint = [...old.endCheckPoint];
this.endCheckPoint[0] += posX;
this.endCheckPoint[1] += posY;
} }
if (posX < this.minX) if (posX < this.minX)
this.minX = posX; this.minX = posX;