added collisions damage and i-frame when taking damage

This commit is contained in:
OfficialCHenry 2022-04-19 01:32:58 -04:00
parent c87c5d9af0
commit a0271d7a97
6 changed files with 89 additions and 9 deletions

View File

@ -18,6 +18,7 @@ import Weapon from "../GameSystems/items/Weapon";
import AnimatedSprite from "../../Wolfie2D/Nodes/Sprites/AnimatedSprite"; import AnimatedSprite from "../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import InputWrapper from "../Tools/InputWrapper"; import InputWrapper from "../Tools/InputWrapper";
import EnemyAI from "../AI/EnemyAI"; import EnemyAI from "../AI/EnemyAI";
import Timer from "../../Wolfie2D/Timing/Timer";
export enum PlayerType { export enum PlayerType {
@ -91,6 +92,9 @@ export default class PlayerController extends StateMachineAI implements BattlerA
// The inventory of the player // The inventory of the player
inventory: InventoryManager; inventory: InventoryManager;
static invincibilityTimer: Timer;
CURRENT_BUFFS: { CURRENT_BUFFS: {
atk: number; //flat value to add to weapon atk: number; //flat value to add to weapon
hp: number; //flat value hp: number; //flat value
@ -110,8 +114,17 @@ export default class PlayerController extends StateMachineAI implements BattlerA
this.CURRENT_SHIELD = newshield; //update shield value this.CURRENT_SHIELD = newshield; //update shield value
} }
else{ else{
(<AnimatedSprite>this.owner).animation.play("HURT", false); //i frame here
PlayerController.invincibilityTimer.start();
this.invincible = true;
(<AnimatedSprite>this.owner).animation.playIfNotAlready("HURT", false);
this.CURRENT_HP -= damage; this.CURRENT_HP -= damage;
if(this.CURRENT_HP <= 0){
this.emitter.fireEvent(Player_Events.PLAYER_KILLED);
(<AnimatedSprite>this.owner).animation.playIfNotAlready("DYING", false);
(<AnimatedSprite>this.owner).animation.queue("DEAD", false);
}
} }
} }
@ -220,8 +233,10 @@ export default class PlayerController extends StateMachineAI implements BattlerA
this.CURRENT_BUFFS = {hp:0, atk:0, def:0, speed:0, range:0}; this.CURRENT_BUFFS = {hp:0, atk:0, def:0, speed:0, range:0};
//to test the buffs //to test the buffs
this.addBuff( {type:BuffType.HEALTH, value:1} ); //this.addBuff( {type:BuffType.HEALTH, value:1} );
//this.addBuff( {type:BuffType.RANGE, value:1, bonus:false} );
//i frame timer
PlayerController.invincibilityTimer = new Timer(400);
} }
initializePlatformer(): void { initializePlatformer(): void {
@ -251,6 +266,9 @@ export default class PlayerController extends StateMachineAI implements BattlerA
update(deltaT: number): void { update(deltaT: number): void {
super.update(deltaT); super.update(deltaT);
if(PlayerController.invincibilityTimer.isStopped()){
this.invincible = false;
}
if(this.currentState instanceof Jump){ if(this.currentState instanceof Jump){
Debug.log("playerstate", "Player State: Jump"); Debug.log("playerstate", "Player State: Jump");

View File

@ -25,6 +25,7 @@ export default abstract class PlayerState extends State {
this.positionTimer.start(); this.positionTimer.start();
PlayerState.dashTimer = new Timer(50); PlayerState.dashTimer = new Timer(50);
PlayerState.dashCoolDownTimer = new Timer(600); PlayerState.dashCoolDownTimer = new Timer(600);
} }
@ -74,5 +75,7 @@ export default abstract class PlayerState extends State {
this.parent.velocity.y += this.gravity*deltaT; this.parent.velocity.y += this.gravity*deltaT;
this.owner.move(this.parent.velocity.scaled(deltaT)); this.owner.move(this.parent.velocity.scaled(deltaT));
} }
} }
} }

View File

@ -33,6 +33,7 @@ import Stack from "../../Wolfie2D/DataTypes/Stack";
import InputWrapper from "../Tools/InputWrapper"; import InputWrapper from "../Tools/InputWrapper";
import Story from "../Tools/DataTypes/Story"; import Story from "../Tools/DataTypes/Story";
import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite"; import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite";
import Platformer from "../../demos/Platformer";
@ -101,6 +102,8 @@ export default class GameLevel extends Scene {
buffs: Array<Buff>; buffs: Array<Buff>;
randomSeed: number; randomSeed: number;
startpos: Vec2;
loadScene(): void { loadScene(): void {
//can load player sprite here //can load player sprite here
@ -202,6 +205,19 @@ export default class GameLevel extends Scene {
if (this.gameStateStack.peek() === GameState.GAMING) { if (this.gameStateStack.peek() === GameState.GAMING) {
switch(event.type){ switch(event.type){
case Player_Events.PLAYER_COLLIDE:
let n = this.sceneGraph.getNode(event.data.get("node"));
let other = this.sceneGraph.getNode(event.data.get("other"));
if(n === this.player){
// Node is player, other is enemy
this.handlePlayerEnemyCollision(<AnimatedSprite>n, <AnimatedSprite>other);
} else {
// Other is player, node is balloon
this.handlePlayerEnemyCollision(<AnimatedSprite>other,<AnimatedSprite>n);
}
break;
case Player_Events.ENEMY_KILLED: case Player_Events.ENEMY_KILLED:
let node = this.sceneGraph.getNode(event.data.get("owner"));//get enemy id let node = this.sceneGraph.getNode(event.data.get("owner"));//get enemy id
@ -225,6 +241,13 @@ export default class GameLevel extends Scene {
this.setGameState(GameState.BUFF); this.setGameState(GameState.BUFF);
this.buffLayer.enable(); this.buffLayer.enable();
break; break;
case Player_Events.PLAYER_KILLED:
//respawn player if he has lives, otherwise end game
console.log("player Died");
(<AnimatedSprite>this.player).animation.play("DEAD", false);
InputWrapper.disableInput();
this.respawnPlayer();
break;
} }
} }
@ -349,6 +372,7 @@ export default class GameLevel extends Scene {
*/ */
protected subscribeToEvents(){ protected subscribeToEvents(){
this.receiver.subscribe([ this.receiver.subscribe([
Player_Events.PLAYER_COLLIDE,
Player_Events.PLAYER_HIT_ENEMY, Player_Events.PLAYER_HIT_ENEMY,
Player_Events.ENEMY_KILLED, Player_Events.ENEMY_KILLED,
Player_Events.LEVEL_START, Player_Events.LEVEL_START,
@ -542,6 +566,7 @@ export default class GameLevel extends Scene {
console.warn("Player spawn was never set - setting spawn to (0, 0)"); console.warn("Player spawn was never set - setting spawn to (0, 0)");
this.playerSpawn = Vec2.ZERO; this.playerSpawn = Vec2.ZERO;
} }
this.startpos = this.playerSpawn;
this.player.position.copy(this.playerSpawn); this.player.position.copy(this.playerSpawn);
this.player.addPhysics(new AABB(Vec2.ZERO, new Vec2(14, 16))); //sets the collision shape this.player.addPhysics(new AABB(Vec2.ZERO, new Vec2(14, 16))); //sets the collision shape
this.player.colliderOffset.set(0, 16); this.player.colliderOffset.set(0, 16);
@ -599,6 +624,7 @@ export default class GameLevel extends Scene {
enemy.addAI(EnemyAI, aiOptions); //TODO - add individual enemy AI enemy.addAI(EnemyAI, aiOptions); //TODO - add individual enemy AI
enemy.setGroup("Enemy"); enemy.setGroup("Enemy");
enemy.setTrigger("player", Player_Events.PLAYER_COLLIDE, null);
//add enemy to the enemy array //add enemy to the enemy array
this.enemies.push(enemy); this.enemies.push(enemy);
@ -676,8 +702,28 @@ export default class GameLevel extends Scene {
} }
/**
* damages the player if they collide with an enemy
* @param player player sprite
* @param enemy enemy sprite
*/
protected handlePlayerEnemyCollision(player: AnimatedSprite, enemy: AnimatedSprite) { protected handlePlayerEnemyCollision(player: AnimatedSprite, enemy: AnimatedSprite) {
//collisions are handled by the battleManager - no need for this in gamelevel for now if(enemy === undefined){
console.log("undefined enemy");
return;
}
if( player === undefined){
console.log("undefined player");
return;
}
if(typeof enemy != undefined && typeof player != undefined){
//damage the player
console.log("player collision damage");
(<PlayerController>this.player._ai).damage(10); //10 collision dmg for now
}
} }
/** /**
@ -699,10 +745,10 @@ export default class GameLevel extends Scene {
* Returns the player to spawn * Returns the player to spawn
*/ */
protected respawnPlayer(): void { protected respawnPlayer(): void {
GameLevel.livesCount = 3;
this.emitter.fireEvent(GameEventType.STOP_SOUND, {key: "level_music"}); this.emitter.fireEvent(GameEventType.STOP_SOUND, {key: "level_music"});
this.sceneManager.changeToScene(MainMenu, {});
InputWrapper.enableInput(); InputWrapper.enableInput();
this.player.position.copy(this.startpos);
(<PlayerController>this.player._ai).CURRENT_HP = (<PlayerController>this.player._ai).MAX_HP + (<PlayerController>this.player._ai).CURRENT_BUFFS.hp;
} }

View File

@ -99,7 +99,18 @@ export default class Levels extends Scene {
if(event.type === "tutorial"){ if(event.type === "tutorial"){
this.sceneManager.changeToScene(Tutorial, {}); let sceneOptions = {
physics: {
groupNames: ["ground", "player", "enemies"],
collisions:
[
[0, 1, 1],
[1, 0, 0],
[1, 0, 0]
]
}
}
this.sceneManager.changeToScene(Tutorial, {}, sceneOptions);
} }
if(event.type === "level1"){ if(event.type === "level1"){

View File

@ -43,6 +43,7 @@ export default class Tutorial extends GameLevel {
startScene(): void { startScene(): void {
this.startpos = this.rmg.getPlayer().scale(32);
// Add the level 1 tilemap // Add the level 1 tilemap
this.add.tilemap("forest1", new Vec2(2, 2)); this.add.tilemap("forest1", new Vec2(2, 2));

View File

@ -10,7 +10,8 @@ export enum Player_Events {
ENEMY_KILLED = "EnemyKilled", ENEMY_KILLED = "EnemyKilled",
PLAYER_HIT_ENEMY = "PlayerHitEnemy", PLAYER_HIT_ENEMY = "PlayerHitEnemy",
BOSS_KILLED = "BossKilled", BOSS_KILLED = "BossKilled",
GIVE_BUFF = "GiveBuff" GIVE_BUFF = "GiveBuff",
PLAYER_COLLIDE = "PlayerCollide"
} }
export enum Damage_Type { export enum Damage_Type {
NORMAL_DAMAGE = "NormalDamage", NORMAL_DAMAGE = "NormalDamage",