Skip to content

Commit

Permalink
fix: ai system
Browse files Browse the repository at this point in the history
  • Loading branch information
ufocoder committed May 24, 2024
1 parent f22b057 commit f392466
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Сharacter {

interface Enemy extends Сharacter {
ai?: boolean;
type: 'zombie' | 'flyguy' | 'soldier' | 'commando' | 'tank' | 'slayer'
sprite: string;
radius: number;
}
Expand Down
17 changes: 12 additions & 5 deletions src/levels/level_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,35 @@ const level: Level = {
5: "DOOR_1E",
},
player: {
x: 1.5,
x: 17.5,
y: 2.5,
angle: 0,
angle: 180,
health: 100,
},
enemies: [
{
ai: true,
type: 'soldier',
sprite: 'soldier',
health: 0,
angle: 10,
x: 18.5,
angle: 180,
x: 14.5,
// x: 18.5,
y: 1.5,
radius: 0.5,
},
/*
{
ai: true,
type: 'soldier',
sprite: 'soldier',
health: 0,
angle: 10,
angle: 180,
x: 18.5,
y: 3.5,
radius: 0.5,
},
*/
],
exit: {
x: 18,
Expand Down
2 changes: 1 addition & 1 deletion src/levels/level_2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const level: Level = {
angle: 90,
health: 100,
},
enemies: [
zombies: [

Check failure on line 58 in src/levels/level_2.ts

View workflow job for this annotation

GitHub Actions / deploy

Object literal may only specify known properties, and 'zombies' does not exist in type 'Level'.
generateZombie(1.5, 9.5),

generateZombie(4.5, 6.5),
Expand Down
10 changes: 4 additions & 6 deletions src/lib/ecs/systems/AISystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ComponentContainer } from "../Component";
import EnemyComponent from "../components/EnemyComponent";
import MoveComponent, { MainDirection } from "../components/MoveComponent";
import AngleComponent from "../components/AngleComponent";
// import { normalizeAngle, radiansToDegrees } from "src/lib/utils";
import { normalizeAngle, radiansToDegrees } from "src/lib/utils";

export default class AISystem extends System {
componentsRequired = new Set([
Expand Down Expand Up @@ -66,7 +66,6 @@ export default class AISystem extends System {

const cameraPosition = camera.get(PositionComponent);
const cameraCircle = camera.get(CircleComponent);
const cameraAngle = camera.get(AngleComponent);
const cameraHealth = camera.get(HealthComponent);

entities.forEach((entity: Entity) => {
Expand All @@ -85,12 +84,11 @@ export default class AISystem extends System {
cameraCircle.radius -
entityCircle?.radius;

if (entityAI.distance > d && d > 0) {
const newAngle = cameraAngle.angle + 180; // normalizeAngle(-radiansToDegrees(Math.acos(dx / d)));

if (entityAI.distance > d && d > 0) {
const angle = radiansToDegrees(Math.atan(dy / dx));
entityAnimation.switchState("walk");
entityMove.mainDirection = MainDirection.Forward;
entityAngle.angle = newAngle;
entityAngle.angle = normalizeAngle(angle);
} else {
entityMove.mainDirection = MainDirection.None;
entityAnimation.switchState("idle");
Expand Down
3 changes: 3 additions & 0 deletions src/lib/ecs/systems/MoveSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export default class MoveSystem extends System {
const mainAngle = degreeToRadians(angleComponent.angle);
const mainCos = Math.cos(mainAngle);
const mainSin = Math.sin(mainAngle);
if (entity === 1) {
console.log('angle', angleComponent.angle, 'cos', mainCos, 'sin', mainSin)
}
const sideAngle = degreeToRadians(angleComponent.angle - 90);
const sideCos = Math.cos(sideAngle);
const sideSin = Math.sin(sideAngle);
Expand Down
30 changes: 22 additions & 8 deletions src/lib/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,28 @@ export function createWorld(ecs: ECS, level: Level, textureManager: TextureManag
ecs.addComponent(entity, new EnemyComponent())

if (enemy.ai) {
ecs.addComponent(entity, new AIComponent(2));
ecs.addComponent(entity, new AnimatedSpriteComponent('idle', {
'attack': animationManager.get('zombieAttack'),
'idle': animationManager.get('zombieIdle'),
'damage': animationManager.get('zombieDamage'),
'death': animationManager.get('zombieDeath'),
'walk': animationManager.get('zombieWalk'),
}))
ecs.addComponent(entity, new AIComponent(20));
}

switch (enemy.type) {
case "zombie":
ecs.addComponent(entity, new AnimatedSpriteComponent('idle', {
'attack': animationManager.get('zombieAttack'),
'idle': animationManager.get('zombieIdle'),
'damage': animationManager.get('zombieDamage'),
'death': animationManager.get('zombieDeath'),
'walk': animationManager.get('zombieWalk'),
}))
break;
case "soldier":
ecs.addComponent(entity, new AnimatedSpriteComponent('idle', {
'attack': animationManager.get('soldierAttack'),
'idle': animationManager.get('soldierIdle'),
'damage': animationManager.get('soldierDamage'),
'death': animationManager.get('soldierDeath'),
'walk': animationManager.get('soldierWalk'),
}))
break;
}

ecs.addComponent(entity, new CircleComponent(enemy.radius));
Expand Down
38 changes: 38 additions & 0 deletions src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,44 @@ export const animation: AnimationSpritePreset[] = [
"./assets/characters/ZombieAttack2.png",
],
},
{
id: "soldierIdle",
frames: [
"./assets/characters/SoldierIdle.png",
],
},
{
id: "soldierWalk",
frames: [
"./assets/characters/SoldierWalk1.png",
"./assets/characters/SoldierWalk2.png",
"./assets/characters/SoldierWalk3.png",
"./assets/characters/SoldierWalk4.png",
],
},
{
id: "soldierDamage",
frames: [
"./assets/characters/SoldierDamage1.png",
"./assets/characters/SoldierDamage2.png",
],
},
{
id: "soldierDeath",
frames: [
"./assets/characters/SoldierDeath1.png",
"./assets/characters/SoldierDeath2.png",
"./assets/characters/SoldierDeath3.png",
"./assets/characters/SoldierDeath4.png",
],
},
{
id: "soldierAttack",
frames: [
"./assets/characters/SoldierAttack1.png",
"./assets/characters/SoldierAttack2.png",
],
},
];


Expand Down
2 changes: 2 additions & 0 deletions src/scenes/LevelScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ECS from "src/lib/ecs";
import { createWorld } from "src/lib/world";
import UISystem from "src/lib/ecs/systems/UISystem";
import HealthComponent from "src/lib/ecs/components/HealthComponent";
import FireSystem from "src/lib/ecs/systems/FireSystem";

Check failure on line 18 in src/scenes/LevelScene.ts

View workflow job for this annotation

GitHub Actions / deploy

Cannot find module 'src/lib/ecs/systems/FireSystem' or its corresponding type declarations.

interface LevelSceneProps {
container: HTMLElement;
Expand All @@ -38,6 +39,7 @@ export default class LevelScene implements BaseScene {
const ecs = new ECS();

ecs.addSystem(new ControlSystem(ecs, container));
ecs.addSystem(new FireSystem(ecs));
ecs.addSystem(new AISystem(ecs, level, soundManager));
ecs.addSystem(new MoveSystem(ecs, level));
ecs.addSystem(new AnimationSystem(ecs));
Expand Down

0 comments on commit f392466

Please sign in to comment.