Skip to content

Commit

Permalink
feat: improve systems
Browse files Browse the repository at this point in the history
  • Loading branch information
ufocoder committed May 31, 2024
1 parent 4590671 commit 6af0246
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/levels/level_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const level: Level = {
enemies: [
generateSoldier(18, 1.75, 4),
generateSoldier(18, 3.25, 4),
...generateZombies(4, 6, 2.5, 0.75, 0.75, 2),
...generateZombies(4, 9.5, 4.5, 1, 0.75, 2),
...generateZombies(6, 6, 2.5, 0.75, 0.75, 2),
...generateZombies(6, 9.5, 4.5, 1, 0.75, 2),
],
exit: {
x: 18,
Expand Down
6 changes: 3 additions & 3 deletions src/levels/level_2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ const level: Level = {
generateZombie(18.5, 2.5, 2),
generateZombie(18.5, 3.5, 2),
generateZombie(18.5, 4.5, 2),
...generateZombies(5, 14.5, 4.5, 1, 1, 2),
...generateZombies(15, 18, 5, 1, 1, 2),
...generateZombies(15, 17, 7, 1, 1, 2),
...generateZombies(15, 14.5, 4.5, 1, 1, 2),
...generateZombies(25, 18, 5, 1, 1, 2),
...generateZombies(25, 17, 7, 1, 1, 2),
],
exit: {
x: 22,
Expand Down
9 changes: 0 additions & 9 deletions src/lib/ecs/components/CollisionComponent.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/lib/ecs/components/WeaponComponent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Component } from "src/lib/ecs/Component";

export default class WeaponComponent implements Component {

bullets: number = 0
damage: number = 5;
frequency: number = 1_000;
lastActionAt: number = +new Date();

constructor(damage: number = 15, frequency: number = 1_000) {
constructor(bullets: number = 30, damage: number = 15, frequency: number = 1_000) {
this.bullets = bullets;
this.damage = damage;
this.frequency = frequency
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ecs/systems/ControlSystem.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import ECS from "src/lib/ecs/ExtendedECS";
import { Entity } from "src/lib/ecs/Entity";
import System from "src/lib/ecs/System";
import MoveComponent, {
MainDirection,
SideDirection,
} from "src/lib/ecs/components/MoveComponent";
import RotateComponent from "src/lib/ecs/components/RotateComponent";
import { Entity } from "src/lib/ecs/Entity";
import ECS from "src/lib/ecs/ExtendedECS";
import ControlComponent from "src/lib/ecs/components/ControlComponent";

const keyCodes: Record<string, string> = {
Expand Down
7 changes: 0 additions & 7 deletions src/lib/ecs/systems/MoveSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import System from "src/lib/ecs/System";
import AngleComponent from "src/lib/ecs/components/AngleComponent";
import MoveComponent from "src/lib/ecs/components/MoveComponent";
import PositionComponent from "src/lib/ecs/components/PositionComponent";
import CollisionComponent from "src/lib/ecs/components/CollisionComponent";
import MapTextureSystem from "./MapTextureSystem";

export default class MoveSystem extends System {
Expand All @@ -23,12 +22,6 @@ export default class MoveSystem extends System {

protected move(dt: number, entity: Entity) {
const components = this.ecs.getComponents(entity)
const collisionComponent = components.get(CollisionComponent);

if (collisionComponent?.isCollided) {
return;
}

const angleComponent = components.get(AngleComponent);
const positionComponent = components.get(PositionComponent);
const { mainDirection, sideDirection, moveSpeed } = components.get(MoveComponent);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/ecs/systems/RotateSystem.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { normalizeAngle } from "src/lib/utils";
import { Entity } from "src/lib/ecs/Entity";
import System from "src/lib/ecs/System";
import { Entity } from "src/lib/ecs/Entity";
import { normalizeAngle } from "src/lib/utils";
import AngleComponent from "src/lib/ecs/components/AngleComponent";
import RotateComponent from "src/lib/ecs/components/RotateComponent";

export default class RotateSystem extends System {
componentsRequired = new Set([AngleComponent, RotateComponent]);
public readonly componentsRequired = new Set([AngleComponent, RotateComponent]);

start(): void {}

Expand Down
29 changes: 23 additions & 6 deletions src/lib/ecs/systems/UISystem.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import ECS from "src/lib/ecs/ExtendedECS";
import System from "src/lib/ecs/System";
import Canvas from "src/lib/Canvas/DefaultCanvas";
import HealthComponent from "src/lib/ecs/components/HealthComponent";
import CameraComponent from "src/lib/ecs/components/CameraComponent";
import Canvas from "src/lib/Canvas/DefaultCanvas";
import WeaponComponent from "src/lib/ecs/components/WeaponComponent";

export default class UISystem extends System {
componentsRequired = new Set([HealthComponent]);
public readonly componentsRequired = new Set([HealthComponent]);

protected readonly width: number = 640;
protected readonly height: number = 480;
Expand Down Expand Up @@ -38,14 +39,30 @@ export default class UISystem extends System {
return;
}

const health = playerContainer.get(HealthComponent);
const weapon = playerContainer.get(WeaponComponent);

this.canvas.clear();
this.canvas.drawText({

if (health) {
this.canvas.drawText({
x: 20,
y: 30,
text: health.current.toString(),
color: 'red',
font: '24px serif',
});
}

if (weapon) {
this.canvas.drawText({
x: 20,
y: 30,
text: playerContainer.get(HealthComponent).current.toString(),
y: 60,
text: weapon.bullets.toString(),
color: 'red',
font: '24px serif',
});
});
}
}

destroy(): void {
Expand Down
8 changes: 7 additions & 1 deletion src/lib/ecs/systems/WeaponSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,18 @@ export default class WeaponSystem extends System {
if (!playerContainer) {
return;
}
const weapon = playerContainer.get(WeaponComponent);

if (weapon.bullets <= 0) {
return
}

this.soundManager.playSound('gun-shot');

const weapon = playerContainer.get(WeaponComponent);
const entity = this.ecs.addEntity();

weapon.bullets -= 1;

this.ecs.addComponent(entity, new BulletComponent(weapon.damage));
this.ecs.addComponent(entity, new PositionComponent(playerContainer.get(PositionComponent).x, playerContainer.get(PositionComponent).y));
this.ecs.addComponent(entity, new AngleComponent(playerContainer.get(AngleComponent).angle));
Expand Down
10 changes: 4 additions & 6 deletions src/lib/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import AnimationManager from "src/managers/AnimationManager";
import BoxComponent from "src/lib/ecs/components/BoxComponent";
import CameraComponent from "src/lib/ecs/components/CameraComponent";
import CircleComponent from "src/lib/ecs/components/CircleComponent";
import CollisionComponent from "src/lib/ecs/components/CollisionComponent";
import ControlComponent from "src/lib/ecs/components/ControlComponent";
import EnemyComponent from "src/lib/ecs/components/EnemyComponent";
import HealthComponent from "src/lib/ecs/components/HealthComponent";
Expand All @@ -29,7 +28,7 @@ export function createEntities(

ecs.addComponent(player, new ControlComponent());
ecs.addComponent(player, new CircleComponent(0.4));
ecs.addComponent(player, new WeaponComponent(10, 1_000));
ecs.addComponent(player, new WeaponComponent(30, 10, 1_000));
ecs.addComponent(
player,
new PositionComponent(level.player.x, level.player.y)
Expand Down Expand Up @@ -71,7 +70,7 @@ export function createEntities(
case "soldier":
ecs.addComponent(
entity,
new WeaponComponent(enemy.attack)
new WeaponComponent(Infinity, enemy.attack)
);
ecs.addComponent(
entity,
Expand Down Expand Up @@ -111,7 +110,7 @@ export function createEntities(
case "commando":
ecs.addComponent(
entity,
new WeaponComponent(enemy.attack)
new WeaponComponent(Infinity, enemy.attack)
);
ecs.addComponent(
entity,
Expand All @@ -127,7 +126,7 @@ export function createEntities(
case "tank":
ecs.addComponent(
entity,
new WeaponComponent(enemy.attack)
new WeaponComponent(Infinity, enemy.attack)
);
ecs.addComponent(
entity,
Expand Down Expand Up @@ -168,7 +167,6 @@ export function createEntities(
const textureName = level.textures[col];
const texture = textureManager.get(textureName);

ecs.addComponent(wall, new CollisionComponent());
ecs.addComponent(wall, new BoxComponent(1));
ecs.addComponent(wall, new PositionComponent(x, y));
ecs.addComponent(wall, new TextureComponent(texture));
Expand Down

0 comments on commit 6af0246

Please sign in to comment.