Create GameLevel.ts
This commit is contained in:
parent
87ff0d5ec7
commit
16370ee776
229
src/shattered_sword/Scenes/GameLevel.ts
Normal file
229
src/shattered_sword/Scenes/GameLevel.ts
Normal file
|
@ -0,0 +1,229 @@
|
|||
import AABB from "../../Wolfie2D/DataTypes/Shapes/AABB";
|
||||
import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
|
||||
import Debug from "../../Wolfie2D/Debug/Debug";
|
||||
import { GameEventType } from "../../Wolfie2D/Events/GameEventType";
|
||||
import Input from "../../Wolfie2D/Input/Input";
|
||||
import { TweenableProperties } from "../../Wolfie2D/Nodes/GameNode";
|
||||
import { GraphicType } from "../../Wolfie2D/Nodes/Graphics/GraphicTypes";
|
||||
import Point from "../../Wolfie2D/Nodes/Graphics/Point";
|
||||
import Rect from "../../Wolfie2D/Nodes/Graphics/Rect";
|
||||
import AnimatedSprite from "../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
|
||||
import Label from "../../Wolfie2D/Nodes/UIElements/Label";
|
||||
import { UIElementType } from "../../Wolfie2D/Nodes/UIElements/UIElementTypes";
|
||||
import Scene from "../../Wolfie2D/Scene/Scene";
|
||||
import Timer from "../../Wolfie2D/Timing/Timer";
|
||||
import Color from "../../Wolfie2D/Utils/Color";
|
||||
import { EaseFunctionType } from "../../Wolfie2D/Utils/EaseFunctions";
|
||||
import PlayerController from "../Player/PlayerController";
|
||||
import MainMenu from "./MainMenu";
|
||||
import { Player_Events } from "../sword_enums";
|
||||
|
||||
// TODO
|
||||
/**
|
||||
* Add in some level music.
|
||||
* This can be done here in the base GameLevel class or individual level files
|
||||
*/
|
||||
export default class GameLevel extends Scene {
|
||||
// Every level will have a player, which will be an animated sprite
|
||||
protected playerSpawn: Vec2;
|
||||
protected player: AnimatedSprite;
|
||||
protected respawnTimer: Timer;
|
||||
|
||||
// Labels for the UI
|
||||
protected static livesCount: number = 3;
|
||||
protected livesCountLabel: Label;
|
||||
|
||||
// Stuff to end the level and go to the next level
|
||||
protected levelEndArea: Rect;
|
||||
protected nextLevel: new (...args: any) => GameLevel;
|
||||
protected levelEndTimer: Timer;
|
||||
protected levelEndLabel: Label;
|
||||
|
||||
// Screen fade in/out for level start and end
|
||||
protected levelTransitionTimer: Timer;
|
||||
protected levelTransitionScreen: Rect;
|
||||
|
||||
startScene(): void {
|
||||
|
||||
// Do the game level standard initializations
|
||||
this.initLayers();
|
||||
this.initViewport();
|
||||
this.initPlayer();
|
||||
this.subscribeToEvents();
|
||||
this.addUI();
|
||||
|
||||
// Initialize the timers
|
||||
this.respawnTimer = new Timer(1000, () => {
|
||||
if(GameLevel.livesCount === 0){
|
||||
this.sceneManager.changeToScene(MainMenu);
|
||||
} else {
|
||||
this.respawnPlayer();
|
||||
this.player.enablePhysics();
|
||||
this.player.unfreeze();
|
||||
}
|
||||
});
|
||||
this.levelTransitionTimer = new Timer(500);
|
||||
this.levelEndTimer = new Timer(3000, () => {
|
||||
// After the level end timer ends, fade to black and then go to the next scene
|
||||
this.levelTransitionScreen.tweens.play("fadeIn");
|
||||
});
|
||||
|
||||
|
||||
// Start the black screen fade out
|
||||
this.levelTransitionScreen.tweens.play("fadeOut");
|
||||
|
||||
// Initially disable player movement
|
||||
Input.disableInput();
|
||||
}
|
||||
|
||||
|
||||
updateScene(deltaT: number){
|
||||
// Handle events and update the UI if needed
|
||||
while(this.receiver.hasNextEvent()){
|
||||
let event = this.receiver.getNextEvent();
|
||||
|
||||
switch(event.type){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialzes the layers
|
||||
*/
|
||||
protected initLayers(): void {
|
||||
// Add a layer for UI
|
||||
this.addUILayer("UI");
|
||||
|
||||
// Add a layer for players and enemies
|
||||
this.addLayer("primary", 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the viewport
|
||||
*/
|
||||
protected initViewport(): void {
|
||||
this.viewport.setZoomLevel(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles all subscriptions to events
|
||||
*/
|
||||
protected subscribeToEvents(){
|
||||
this.receiver.subscribe([
|
||||
Player_Events.PLAYER_HIT_ENEMY,
|
||||
Player_Events.ENEMY_KILLED,
|
||||
Player_Events.LEVEL_START,
|
||||
Player_Events.LEVEL_END,
|
||||
Player_Events.PLAYER_KILLED
|
||||
]);
|
||||
}
|
||||
|
||||
// TODO -
|
||||
/**
|
||||
* Adds in any necessary UI to the game
|
||||
*/
|
||||
protected addUI(){
|
||||
// In-game labels
|
||||
|
||||
// End of level label (start off screen)
|
||||
this.levelEndLabel = <Label>this.add.uiElement(UIElementType.LABEL, "UI", {position: new Vec2(-300, 200), text: "Level Complete"});
|
||||
this.levelEndLabel.size.set(1200, 60);
|
||||
this.levelEndLabel.borderRadius = 0;
|
||||
this.levelEndLabel.backgroundColor = new Color(34, 32, 52);
|
||||
this.levelEndLabel.textColor = Color.WHITE;
|
||||
this.levelEndLabel.fontSize = 48;
|
||||
this.levelEndLabel.font = "PixelSimple";
|
||||
|
||||
// Add a tween to move the label on screen
|
||||
this.levelEndLabel.tweens.add("slideIn", {
|
||||
startDelay: 0,
|
||||
duration: 1000,
|
||||
effects: [
|
||||
{
|
||||
property: TweenableProperties.posX,
|
||||
start: -300,
|
||||
end: 300,
|
||||
ease: EaseFunctionType.OUT_SINE
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the player
|
||||
*/
|
||||
protected initPlayer(): void {
|
||||
// Add the player
|
||||
this.player = this.add.animatedSprite("player", "primary");
|
||||
this.player.scale.set(2, 2);
|
||||
if(!this.playerSpawn){
|
||||
console.warn("Player spawn was never set - setting spawn to (0, 0)");
|
||||
this.playerSpawn = Vec2.ZERO;
|
||||
}
|
||||
this.player.position.copy(this.playerSpawn);
|
||||
this.player.addPhysics(new AABB(Vec2.ZERO, new Vec2(14, 14)));
|
||||
this.player.colliderOffset.set(0, 2);
|
||||
this.player.addAI(PlayerController, {playerType: "platformer", tilemap: "Main"});
|
||||
|
||||
this.player.setGroup("player");
|
||||
|
||||
this.viewport.follow(this.player);
|
||||
}
|
||||
|
||||
|
||||
//TODO -
|
||||
/**
|
||||
* Adds an Enemy into the game
|
||||
* @param spriteKey The key of the Enemy sprite
|
||||
* @param tilePos The tilemap position to add the Enemy to
|
||||
* @param aiOptions The options for the Enemy AI
|
||||
*/
|
||||
/*
|
||||
protected addEnemy(spriteKey: string, tilePos: Vec2, aiOptions: Record<string, any>): void {
|
||||
let enemy = this.add.animatedSprite(spriteKey, "primary");
|
||||
enemy.position.set(tilePos.x*32, tilePos.y*32);
|
||||
enemy.scale.set(2, 2);
|
||||
enemy.addPhysics();
|
||||
enemy.addAI(EnemyController, aiOptions); //TODO - add individual enemy AI
|
||||
enemy.setGroup("Enemy");
|
||||
|
||||
enemy.setTrigger("player",Player_Events.PLAYER_HIT_ENEMY, null);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
protected handlePlayerEnemyCollision(player: AnimatedSprite, enemy: AnimatedSprite) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the amount of life the player has
|
||||
* @param amt The amount to add to the player life
|
||||
*/
|
||||
protected incPlayerLife(amt: number): void {
|
||||
GameLevel.livesCount += amt;
|
||||
this.livesCountLabel.text = "Lives: " + GameLevel.livesCount;
|
||||
if (GameLevel.livesCount == 0){
|
||||
Input.disableInput();
|
||||
this.player.disablePhysics();
|
||||
this.emitter.fireEvent(GameEventType.PLAY_SOUND, {key: "player_death", loop: false, holdReference: false});
|
||||
this.player.tweens.play("death");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the player to spawn
|
||||
*/
|
||||
protected respawnPlayer(): void {
|
||||
GameLevel.livesCount = 3;
|
||||
this.emitter.fireEvent(GameEventType.STOP_SOUND, {key: "level_music"});
|
||||
this.sceneManager.changeToScene(MainMenu, {});
|
||||
Input.enableInput();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user