Skip to content

Commit

Permalink
simple enemy
Browse files Browse the repository at this point in the history
  • Loading branch information
androettop committed Nov 6, 2024
1 parent 4afe228 commit c58cd08
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 13 deletions.
44 changes: 44 additions & 0 deletions src/actors/enemies/enemy_soldier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Engine, Vector } from "excalibur";
import BaseSoldier from "../soldier/base_soldier";
import { SoldierResources } from "../soldier/resources";

export interface EnemySoldierParams {
pos: Vector;
}

class EnemySoldier extends BaseSoldier {
constructor({ pos }: EnemySoldierParams) {
super({
pos,
spriteImageSource: SoldierResources.EnemySprites,
});
}

public soldierInput() {
/*
move right: this.direction = 1; this.isRunning = true;
move left: this.direction = -1; this.isRunning = true;
stop moving: this.isRunning = false;
shoot this.activeWeapon?.shoot();
*/

// move for 200px to the right and then to the left
const baseX = 250;
this.isRunning = true;
if (this.direction === 1 && this.pos.x > baseX + 200) {
this.direction = -1;
} else if (this.direction === -1 && this.pos.x < baseX) {
this.direction = 1;
}
}

public onDie() {}

public onInitialize(engine: Engine) {
super.onInitialize(engine);
// TODO: remove this
(window as any).player = this;
}
}

export default EnemySoldier;
6 changes: 3 additions & 3 deletions src/actors/player/player.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Engine, Vector } from "excalibur";
import { GAME_CONTROLS } from "../../helpers/consts";
import BaseSoldier from "./base_soldier";
import { PlayerResources } from "./resources";
import BaseSoldier from "../soldier/base_soldier";
import { SoldierResources } from "../soldier/resources";

export interface PlayerParams {
pos: Vector;
Expand All @@ -12,7 +12,7 @@ class Player extends BaseSoldier {
super({
pos,
name: "player",
spriteImageSource: PlayerResources.PlayerSprites,
spriteImageSource: SoldierResources.PlayerSprites,
});
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ abstract class BaseSoldier extends Actor {
);
}

abstract soldierInput(engine: Engine): void;
abstract soldierInput(engine: Engine, delta: number): void;

private updateGroundedState() {
// All the game surfaces are flat, so we can just check if the soldier is moving vertically
Expand All @@ -126,8 +126,8 @@ abstract class BaseSoldier extends Actor {
}
}

private updateSoldierState(engine: Engine) {
this.soldierInput(engine);
private updateSoldierState(engine: Engine, delta: number) {
this.soldierInput(engine, delta);
this.updateGroundedState();
}

Expand Down Expand Up @@ -181,7 +181,7 @@ abstract class BaseSoldier extends Actor {
}

// Get the new state for the soldier
this.updateSoldierState(engine);
this.updateSoldierState(engine, delta);

// Execute the soldier actions based on the state
this.executeSoldierActions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { greenToRed } from "../../helpers/colors";

const basePlayerSprite = new ImageSource(playerSprites);

// Cargar la imagen original
export const PlayerResources = {
export const SoldierResources = {
PlayerSprites: basePlayerSprite,
EnemySprites: await greenToRed(playerSprites),
} as const;
Expand Down
2 changes: 1 addition & 1 deletion src/actors/weapons/wp_pistol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Player from "../player/player";
import PBullet from "../projectile/p_bullet";
import { Engine, SpriteSheet, vec } from "excalibur";
import { SoundResources } from "../sounds/resources";
import BaseSoldier from "../player/base_soldier";
import BaseSoldier from "../soldier/base_soldier";

class WpPistol extends Weapon {
public readonly twoHanded = false;
Expand Down
2 changes: 1 addition & 1 deletion src/actors/weapons/wp_shotgun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Player from "../player/player";
import PBullet from "../projectile/p_bullet";
import { Engine, SpriteSheet, vec } from "excalibur";
import { SoundResources } from "../sounds/resources";
import BaseSoldier from "../player/base_soldier";
import BaseSoldier from "../soldier/base_soldier";

class WpShotgun extends Weapon {
public readonly twoHanded = true;
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { createLoader } from "./helpers/resources";
import { UiResources } from "./actors/ui/resources";
import { SoundResources } from "./actors/sounds/resources";
import { WorldResources } from "./actors/world/resources";
import { PlayerResources } from "./actors/player/resources";
import { TextResources } from "./actors/text/resources";
import { ProjectileResources } from "./actors/projectile/resources";
import { SoldierResources } from "./actors/soldier/resources";

class Game extends Engine {
constructor() {
Expand All @@ -31,7 +31,7 @@ class Game extends Engine {
UiResources,
SoundResources,
WorldResources,
PlayerResources,
SoldierResources,
TextResources,
ProjectileResources,
),
Expand Down
9 changes: 9 additions & 0 deletions src/scenes/debug_scene/debug_scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import StaticImage from "../../actors/ui/static_image";
import { backgroundSpriteSheet } from "../../actors/world/resources";
import WeaponsIndicator from "../../actors/ui/weapons_indicator";
import DirtBlock from "../../actors/world/dirt_block";
import EnemySoldier from "../../actors/enemies/enemy_soldier";
class DebugScene extends Scene {
private player: Player | null = null;

Expand Down Expand Up @@ -38,6 +39,13 @@ class DebugScene extends Scene {
this.add(this.player);
}

private initEnemies() {
const soldier = new EnemySoldier({
pos: vec(GAME_WIDTH / 2, GAME_HEIGHT / 2),
});
this.add(soldier);
}

private initMap() {
// add 2 rows of DirtBlock (64x64) at the bottom of the screen
for (let i = 0; i < Math.ceil(GAME_WIDTH / 64); i++) {
Expand Down Expand Up @@ -86,6 +94,7 @@ class DebugScene extends Scene {
public onInitialize() {
this.initBackground();
this.initMap();
this.initEnemies();
this.initPlayer();
this.initHUD();
}
Expand Down

0 comments on commit c58cd08

Please sign in to comment.