Skip to content

Commit

Permalink
chore: pass player state between levels
Browse files Browse the repository at this point in the history
ref #28
  • Loading branch information
ufocoder committed Aug 24, 2024
1 parent 92fda72 commit 3fec1ec
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 107 deletions.
3 changes: 0 additions & 3 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ interface Item {
value: number;
}


interface Character {
x: number;
y: number;
Expand Down Expand Up @@ -137,7 +136,5 @@ type Vector2D = {
type PlayerState = {
ammo?: number;
health: number;
soundMuted: boolean;
timeLeft?: number;
}

27 changes: 0 additions & 27 deletions src/levels/generators.ts → src/levels/generators/characters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,6 @@ const random = (from: number, to: number) => {
return from + Math.random() * (to - from);
};

export const generatePistol = (x: number, y: number, value: number) =>
({
type: 'pistol_weapon',
radius: 0.3,
x,
y,
value,
}) as Item;

export const generatePistolAmmo = (x: number, y: number, value: number) =>
({
type: 'pistol_ammo',
radius: 0.3,
x,
y,
value,
}) as Item;

export const generateHealthPack = (x: number, y: number, value: number) =>
({
type: 'health_pack',
radius: 0.3,
x,
y,
value,
}) as Item;

export const generateEntities =
<T>(generator: (x: number, y: number, ai: number) => T) =>
(limit: number, x: number, y: number, dx: number, dy: number, ai: number = 0) => {
Expand Down
26 changes: 26 additions & 0 deletions src/levels/generators/components.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import AnimatedSpriteComponent from "src/lib/ecs/components/AnimatedSpriteComponent";
import WeaponMeleeComponent from "src/lib/ecs/components/WeaponMeleeComponent";
import WeaponRangeComponent from "src/lib/ecs/components/WeaponRangeComponent";
import AnimationManager from "src/managers/AnimationManager";

export const generateKnifeWeapon = (animationManager: AnimationManager) => new WeaponMeleeComponent({
sprite: new AnimatedSpriteComponent('idle', {
attack: animationManager.get("knifeAttack"),
idle: animationManager.get("knifeIdle"),
}),
attackDamage: 30,
attackFrequency: 500,
});

export const generatePistolWeapon = (animationManager: AnimationManager, bulletTotal: number = 30) => new WeaponRangeComponent({
bulletTotal,
bulletSprite: "pistol_bullet",
bulletDamage: 100,
bulletSpeed: 15,
attackDistance: 15,
attackFrequency: 250,
sprite: new AnimatedSpriteComponent("idle", {
attack: animationManager.get("pistolAttack"),
idle: animationManager.get("pistolIdle"),
}),
});
26 changes: 26 additions & 0 deletions src/levels/generators/items.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const generatePistol = (x: number, y: number, value: number) =>
({
type: "pistol_weapon",
radius: 0.3,
x,
y,
value,
}) as Item;

export const generatePistolAmmo = (x: number, y: number, value: number) =>
({
type: "pistol_ammo",
radius: 0.3,
x,
y,
value,
}) as Item;

export const generateHealthPack = (x: number, y: number, value: number) =>
({
type: "health_pack",
radius: 0.3,
x,
y,
value,
}) as Item;
3 changes: 2 additions & 1 deletion src/levels/level_1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generatePistolAmmo, generatePistol, generateSoldier, generateZombies } from "./generators";
import { generateSoldier, generateZombies } from "./generators/characters";
import { generatePistolAmmo, generatePistol } from "./generators/items";

const level: Level = {
world: {
Expand Down
3 changes: 2 additions & 1 deletion src/levels/level_2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generatePistolAmmo, generateHealthPack, generateSoldier, generateZombie, generateZombies } from "./generators";
import { generateSoldier, generateZombie, generateZombies } from "./generators/characters";
import { generatePistolAmmo, generateHealthPack } from "./generators/items";

const level: Level = {
world: {
Expand Down
3 changes: 2 additions & 1 deletion src/levels/level_3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generatePistolAmmo, generateFlygies, generateSoldiers, generateZombies } from "./generators";
import { generateFlygies, generateSoldiers, generateZombies } from "./generators/characters";
import { generatePistolAmmo } from "./generators/items";

const level: Level = {
world: {
Expand Down
3 changes: 2 additions & 1 deletion src/levels/level_final.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generatePistolAmmo, generateCircle, generateTank, generateZombie, generateZombies } from "./generators";
import { generateCircle, generateTank, generateZombie, generateZombies } from "./generators/characters";
import { generatePistolAmmo } from "./generators/items";

const level: Level = {
world: {
Expand Down
15 changes: 2 additions & 13 deletions src/lib/ecs/systems/MapItemSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import PlayerComponent from "src/lib/ecs/components/PlayerComponent";
import PositionComponent from "src/lib/ecs/components/PositionComponent";
import HealthComponent from "src/lib/ecs/components/HealthComponent";
import WeaponRangeComponent from "src/lib/ecs/components/WeaponRangeComponent";
import AnimatedSpriteComponent from "src/lib/ecs/components/AnimatedSpriteComponent";
import { WEAPON_PISTOL_INDEX } from "./WeaponSystem";
import { generatePistolWeapon } from "src/levels/generators/components";

export default class MapItemSystem extends System {
public readonly componentsRequired = new Set([
Expand Down Expand Up @@ -58,18 +58,7 @@ export default class MapItemSystem extends System {
Math.floor(playerPosition.y) === Math.floor(entityPosition.y)
) {
if (!playerWeapon && entityItem.type === "pistol_weapon") {
const pistolWeapon = new WeaponRangeComponent({
bulletSprite: "pistol_bullet",
bulletTotal: 30,
bulletDamage: 100,
bulletSpeed: 15,
attackDistance: 15,
attackFrequency: 250,
sprite: new AnimatedSpriteComponent("idle", {
attack: this.animationManager.get("pistolAttack"),
idle: this.animationManager.get("pistolIdle"),
}),
});
const pistolWeapon = generatePistolWeapon(this.animationManager);

this.soundManager.playSound("pick");

Expand Down
24 changes: 13 additions & 11 deletions src/lib/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,33 @@ import SpriteComponent from "./ecs/components/SpriteComponent";
import PlayerComponent from "./ecs/components/PlayerComponent";
import ItemComponent from "./ecs/components/ItemComponent";
import DoorComponent from "src/lib/ecs/components/DoorComponent.ts";
import { WEAPON_KNIFE_INDEX } from "./ecs/systems/WeaponSystem";
import { WEAPON_KNIFE_INDEX, WEAPON_PISTOL_INDEX } from "./ecs/systems/WeaponSystem";
import { generateKnifeWeapon, generatePistolWeapon } from "src/levels/generators/components";

export function createLevelEntities(
ecs: ECS,
level: Level,
playerState: PlayerState,
textureManager: TextureManager,
animationManager: AnimationManager
) {

const player = ecs.addEntity();

const playerHealth = playerState.health || level.player.health;
const playerComponent = new PlayerComponent();

const knifeWeapon = new WeaponMeleeComponent({
sprite: new AnimatedSpriteComponent('idle', {
attack: animationManager.get("knifeAttack"),
idle: animationManager.get("knifeIdle"),
}),
attackDamage: 30,
attackFrequency: 500,
});
const knifeWeapon = generateKnifeWeapon(animationManager);

playerComponent.currentWeapon = knifeWeapon;
playerComponent.weapons[WEAPON_KNIFE_INDEX] = knifeWeapon;

if (playerState.ammo) {
const pistolWeapon = generatePistolWeapon(animationManager, playerState.ammo);

playerComponent.currentWeapon = pistolWeapon;
playerComponent.weapons[WEAPON_PISTOL_INDEX] = pistolWeapon;
}

ecs.addComponent(player, playerComponent);
ecs.addComponent(player, new ControlComponent());
ecs.addComponent(player, new CircleComponent(0.4));
Expand All @@ -56,7 +58,7 @@ export function createLevelEntities(
);
ecs.addComponent(
player,
new HealthComponent(level.player.health, level.player.health)
new HealthComponent(playerHealth, playerHealth)
);
ecs.addComponent(player, new AngleComponent(level.player.angle));
ecs.addComponent(player, new MoveComponent(3, true));
Expand Down
17 changes: 11 additions & 6 deletions src/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ export function createScenario({
}: ScenarioProps) {
let levelIndex = 0;

const playerState: PlayerState = {
health: 100,
};

const showFinalScene = () => {
const scene = new TitleScene(container, "Congratulation!", ["You survived a zombie invasion"]);
const scene = new TitleScene(container, playerState, "Congratulation!", ["You survived a zombie invasion"]);
scene.start();
};

const showFailedScene = () => {
const scene = new TitleScene(container, "You died");
const scene = new TitleScene(container, playerState, "You died");
scene.start();
};

const switchToLevelNextScene = () => {
const switchToLevelNextScene = (playerState: PlayerState) => {
const level = levels[levelIndex];

if (!level) {
Expand All @@ -47,14 +51,15 @@ export function createScenario({
soundManager,
textureManager,
animationManager,
playerState,
});

scene.onComplete(() => {
if (level.music) {
soundManager.pauseBackground();
}
scene.destroy();
switchToLevelNextScene();
switchToLevelNextScene(scene.playerState);
});

scene.onFailed(() => {
Expand All @@ -72,7 +77,7 @@ export function createScenario({
}
};

const startScene = new TitleScene(container, "Shoot or run",
const startScene = new TitleScene(container, playerState, "Shoot or run",
[
"Use WASD and mouse to play",
"Use M to mute",
Expand All @@ -82,7 +87,7 @@ export function createScenario({

startScene.onComplete(() => {
startScene.destroy();
switchToLevelNextScene();
switchToLevelNextScene(playerState);
});

startScene.start();
Expand Down
1 change: 1 addition & 0 deletions src/scenes/BaseScene.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default interface BaseScene {
playerState: PlayerState;
onComplete(cb: () => void): void;
start(): void;
destroy(): void;
Expand Down
Loading

0 comments on commit 3fec1ec

Please sign in to comment.