Skip to content

Commit

Permalink
- dragon hurt + refactor + boss class
Browse files Browse the repository at this point in the history
  • Loading branch information
Robouste committed Jan 12, 2025
1 parent 3a63350 commit 9e79ba4
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 77 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/sprites/dragon/dragon-spritesheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/enums/game-scene-tag.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum GameSceneTag {
OBSTICLE = "obsticle_scene_tag",
CLOUD = "cloud_scene_tag",
DRAGON = "dragon_scene_tag",
ANCIENT_DRAGON = "ancient_dragon_scene_tag",
GROUND = "ground_scene_tag",
LEVEL = "level_scene_tag",
}
31 changes: 16 additions & 15 deletions src/enums/sprite-name.enum.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
export enum SpriteName {
PLAYER = "player_sprite",
CLOUD = "cloud_sprite",
OBSTICLE_1 = "obsticle_1_sprite",
OBSTICLE_2 = "obsticle_2_sprite",
ANCIENT_DRAGON = "ancient_dragon_sprite",
BACKGROUND = "background_sprite",
BULLET = "bullet_sprite",
BULLET_IMPACT = "bullet_impact_sprite",
INVINCIBLE_IMPACT = "invincible_impact_sprite",
DRAGON = "dragon_sprite",
CONTAINER = "container_sprite",
BUTTON_PRIMARY = "button_primary_sprite",
BUTTON = "button_sprite",
FLOOR_ROCK_1 = "floor_rock_1_sprite",
BUTTON_PRIMARY = "button_primary_sprite",
CLOUD = "cloud_sprite",
CONTAINER = "container_sprite",
DRAGON = "dragon_sprite",
FLOOR_GRASS_1 = "floor_grass_1_sprite",
FLOOR_ROCK_2 = "floor_rock_2_sprite",
FLOOR_GRASS_2 = "floor_grass_2_sprite",
FLOOR_ROCK_3 = "floor_rock_3_sprite",
FLOOR_GRASS_3 = "floor_grass_3_sprite",
FLOOR_ROCK_4 = "floor_rock_4_sprite",
FLOOR_GRASS_4 = "floor_grass_4_sprite",
FLOOR_ROCK_5 = "floor_rock_5_sprite",
FLOOR_GRASS_5 = "floor_grass_5_sprite",
FLOOR_ROCK_6 = "floor_rock_6_sprite",
FLOOR_GRASS_6 = "floor_grass_6_sprite",
BACKGROUND = "background_sprite",
FLOOR_ROCK_1 = "floor_rock_1_sprite",
FLOOR_ROCK_2 = "floor_rock_2_sprite",
FLOOR_ROCK_3 = "floor_rock_3_sprite",
FLOOR_ROCK_4 = "floor_rock_4_sprite",
FLOOR_ROCK_5 = "floor_rock_5_sprite",
FLOOR_ROCK_6 = "floor_rock_6_sprite",
GAME_OVER = "game_over_sprite",
INVINCIBLE_IMPACT = "invincible_impact_sprite",
OBSTICLE_1 = "obsticle_1_sprite",
OBSTICLE_2 = "obsticle_2_sprite",
PLAYER = "player_sprite",
}
22 changes: 21 additions & 1 deletion src/helpers/assets-loader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,35 @@ export class AssetsLoader {
);
loadSprite(SpriteName.DRAGON, "sprites/dragon/dragon-spritesheet.png", {
sliceX: 4,
sliceY: 1,
sliceY: 2,
anims: {
fly: {
from: 0,
to: 3,
loop: true,
},
hurt: {
from: 4,
to: 5,
loop: false,
},
},
});
loadSprite(
SpriteName.ANCIENT_DRAGON,
"sprites/dragon/ancient-dragon-spritesheet.png",
{
sliceX: 4,
sliceY: 1,
anims: {
fly: {
from: 0,
to: 3,
loop: true,
},
},
}
);
loadSprite(
SpriteName.BULLET_IMPACT,
"sprites/bullet/bullet-impact-spritesheet.png",
Expand Down
36 changes: 36 additions & 0 deletions src/objects/ennemies/ancient-dragon.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { GameSceneTag } from "../../enums/game-scene-tag.enum";
import { SpriteName } from "../../enums/sprite-name.enum";
import { DragonComp } from "../../types/ennemy.type";
import { Ennemy } from "../ennemy.class";

export class AncientDragon extends Ennemy<DragonComp> {
protected spriteName = SpriteName.ANCIENT_DRAGON;

constructor(protected onCollide: () => void) {
super();

const dragonHeight = 196;

this.ref = make([
sprite(this.spriteName, {
height: dragonHeight,
}),
area(),
pos(width(), height() / 2),
health(500),
z(98),
GameSceneTag.ANCIENT_DRAGON,
{
speed: 50,
},
]);

this.ref.onUpdate(() => {
if (this.ref.pos.x > width() - 256) {
this.ref.moveTo(width() - 256, this.ref.pos.y, this.ref.speed);
}
});

this.ref.play("fly");
}
}
38 changes: 34 additions & 4 deletions src/objects/ennemies/dragon.class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OBSTICLE_GROUND_OFFSET, OBSTICLE_HEIGHT } from "../../constants";
import { GameSceneTag } from "../../enums/game-scene-tag.enum";
import { PlayerTag } from "../../enums/player-tag.enum";
import { SoundTag } from "../../enums/sound.enum";
import { SpriteName } from "../../enums/sprite-name.enum";
import { DragonSpawnSettings } from "../../types/difficulty-config.type";
import { DragonComp } from "../../types/ennemy.type";
Expand All @@ -12,7 +13,6 @@ export class Dragon extends Ennemy<DragonComp> {
protected spriteName = SpriteName.DRAGON;

constructor(
protected onCollide: () => void,
private _spawnSettings: DragonSpawnSettings,
private _gameConfig: GameConfig
) {
Expand Down Expand Up @@ -54,14 +54,44 @@ export class Dragon extends Ennemy<DragonComp> {
// prevent dragon from going behind the obsticles
const posY = Math.min(playerPosY, minPosY);

this.ref.moveTo(-100, posY, this._spawnSettings.speed);
this.ref.moveTo(-500, posY, this._spawnSettings.speed);
});

this.ref.play("fly");

this.ref.onCollide(PlayerTag.PLAYER, () => {
this.ref.onCollide(PlayerTag.PLAYER, (player) => {
this.ref.destroy();
this.onCollide();
player.destroy();
});

this.ref.onCollide(PlayerTag.BULLET, (bullet) => {
play(SoundTag.IMPACT, {
volume: 0.7,
});

this.ref.hurt(30);

const isDead = this.ref.hp() <= 0;

const impact = add([
sprite(SpriteName.BULLET_IMPACT),
pos(bullet.pos.x, bullet.pos.y - bullet.height),
area(),
move(LEFT, this.ref.speed),
]);

if (isDead) {
this.ref.destroy();
}

this.ref.play("hurt", {
onEnd: () => this.ref.play("fly"),
});

impact.play("impact");

impact.onAnimEnd(() => impact.destroy());
bullet.destroy();
});
}
}
26 changes: 22 additions & 4 deletions src/objects/ennemies/obsticle.class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OBSTICLE_GROUND_OFFSET, OBSTICLE_HEIGHT } from "../../constants";
import { GameSceneTag } from "../../enums/game-scene-tag.enum";
import { PlayerTag } from "../../enums/player-tag.enum";
import { SoundTag } from "../../enums/sound.enum";
import { SpriteName } from "../../enums/sprite-name.enum";
import { SpawnSettings } from "../../types/difficulty-config.type";
import { ObsticleComp } from "../../types/ennemy.type";
Expand All @@ -11,8 +12,7 @@ export class Obsticle extends Ennemy<ObsticleComp> {
constructor(
protected spriteName: SpriteName,
private _spawnSettings: SpawnSettings,
private _config: GameConfig,
protected onCollide: () => void
private _config: GameConfig
) {
super();

Expand Down Expand Up @@ -57,9 +57,27 @@ export class Obsticle extends Ennemy<ObsticleComp> {
);
});

this.ref.onCollide(PlayerTag.PLAYER, () => {
this.ref.onCollide(PlayerTag.PLAYER, (player) => {
this.ref.destroy();
this.onCollide();
player.destroy();
});

this.ref.onCollide(PlayerTag.BULLET, (bullet) => {
play(SoundTag.IMPACT_INVINCIBLE, {
volume: 0.8,
});

const impact = add([
sprite(SpriteName.INVINCIBLE_IMPACT),
pos(bullet.pos.x, bullet.pos.y - bullet.height),
area(),
move(LEFT, this.ref.speed),
]);

impact.onAnimEnd(() => impact.destroy());

impact.play("impact");
bullet.destroy();
});
}
}
1 change: 0 additions & 1 deletion src/objects/ennemy.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { EnnemyComp } from "../types/ennemy.type";

export abstract class Ennemy<T extends EnnemyComp> {
public ref: T;
protected abstract onCollide: () => void;
protected abstract spriteName: SpriteName;

public add(): void {
Expand Down
44 changes: 0 additions & 44 deletions src/objects/player.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { PlayerTag } from "../enums/player-tag.enum";
import { SoundTag } from "../enums/sound.enum";
import { SpriteName } from "../enums/sprite-name.enum";
import { DebugHelper } from "../helpers/debug.helper";
import { DragonComp, ObsticleComp } from "../types/ennemy.type";
import { GameConfig } from "../types/game-config.type";
import { PlayerComp } from "../types/player.type";

Expand Down Expand Up @@ -101,48 +100,5 @@ export class Player {
]);

bullet.play("move");

bullet.onCollide(GameSceneTag.OBSTICLE, (obsticle: ObsticleComp) => {
play(SoundTag.IMPACT_INVINCIBLE, {
volume: 0.8,
});

const impact = add([
sprite(SpriteName.INVINCIBLE_IMPACT),
pos(bullet.pos.x, bullet.pos.y - bullet.height),
area(),
move(LEFT, obsticle.speed),
]);

impact.onAnimEnd(() => impact.destroy());

impact.play("impact");
destroy(bullet);
});

bullet.onCollide(GameSceneTag.DRAGON, (dragon: DragonComp) => {
play(SoundTag.IMPACT, {
volume: 0.7,
});

dragon.hurt(30);

const dragonIsDead = dragon.hp() <= 0;

const impact = add([
sprite(SpriteName.BULLET_IMPACT),
pos(bullet.pos.x, bullet.pos.y - bullet.height),
area(),
move(LEFT, dragon.speed),
]);

if (dragonIsDead) {
dragon.destroy();
}

impact.play("impact");
impact.onAnimEnd(() => impact.destroy());
destroy(bullet);
});
}
}
15 changes: 7 additions & 8 deletions src/scenes/game.scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class GameScene {
this._levelLabel = add([
text(`Level: ${this._level.value}`),
pos(24, 60),
z(100),
]);
setGravity(1800);

Expand All @@ -77,6 +78,8 @@ export class GameScene {
wait(2, () => this.spawnDragon());
this.spawnClouds();
this.addScore();

this._player.ref.onDestroy(() => this.gameOver());
}

private addBackground(): void {
Expand Down Expand Up @@ -117,8 +120,7 @@ export class GameScene {
const obsticle = new Obsticle(
spriteName,
obsticleDifficulty,
this._config,
() => this.gameOver()
this._config
);

obsticle.add();
Expand All @@ -137,11 +139,7 @@ export class GameScene {
loop(
0.3,
() => {
const dragon = new Dragon(
() => this.gameOver(),
dragonDifficulty,
this._config
);
const dragon = new Dragon(dragonDifficulty, this._config);
dragon.add();
},
dragonDifficulty.amount
Expand All @@ -166,7 +164,7 @@ export class GameScene {
}

private addScore(): void {
const scoreLabel = add([text(this.score), pos(24, 24)]);
const scoreLabel = add([text(this.score), pos(24, 24), z(100)]);

loop(0.01, () => {
this._score++;
Expand Down Expand Up @@ -202,6 +200,7 @@ export class GameScene {
pos(center()),
anchor("center"),
animate(),
z(100),
]);

levelUpText.animate(
Expand Down

0 comments on commit 9e79ba4

Please sign in to comment.