Skip to content

Commit 547592c

Browse files
committed
Enemy turns (looks) towards the Player when attacking
1 parent 87a1ec5 commit 547592c

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

src/app/domain/Creature.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as CONFIG from '@app/configuration/config.json'
22

3+
import { angleBetweenPoints } from '@app/infrastructure/geometry/Point'
34
import GameTime from '@app/infrastructure/GameTime'
45
import CollisionBox, { collisionBoxesIntersect, ICollidable } from '@app/infrastructure/CollisionBox'
5-
import { Directions, MovingDirections } from '@app/infrastructure/Directions'
6+
import { Directions, MovingDirections, getDirectionBasedOnAngle } from '@app/infrastructure/Directions'
67

78
import Map from '@app/domain/map/Map'
89
import CreatureState from '@app/domain/CreatureState'
@@ -375,7 +376,16 @@ export default abstract class Creature {
375376
if (this.prevY.length > this.prevHistoryLength) { this.prevY.shift() }
376377
}
377378

378-
protected updateDirection(): void {
379+
protected updateDirection(targetCreature: Creature): void {
380+
if (this.state === CreatureState.Attacking) {
381+
this.updateDirectionWhenAttacking(targetCreature)
382+
}
383+
else {
384+
this.updateDirectionWhenMoving()
385+
}
386+
}
387+
388+
private updateDirectionWhenMoving(): void {
379389
const direction: string[] = []
380390

381391
if (this.movingDirections.down && this.blocked.down === false) {
@@ -397,6 +407,11 @@ export default abstract class Creature {
397407
this.direction = Directions[directionString as keyof typeof Directions]
398408
}
399409

410+
private updateDirectionWhenAttacking(targetCreature: Creature): void {
411+
const theta = angleBetweenPoints(targetCreature, this)
412+
this.direction = getDirectionBasedOnAngle(theta)
413+
}
414+
400415
protected checkIfMoving(): boolean {
401416
// Check if all of the recorded prevX & prevY positions are the same
402417
const xUnchanged = this.prevX.every((prevX, i) => (i === 0) ? true : (prevX === this.prevX[0]))

src/app/domain/enemies/ConcreteEnemy/ConcreteEnemy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default class ConcreteEnemy extends Enemy {
133133

134134
this.adjustCollisionWithWalls() // Must come after move()
135135

136-
this.updateDirection() // Must come after adjustCollisionWithWalls()
136+
this.updateDirection(player) // Must come after adjustCollisionWithWalls()
137137

138138
super.update(player)
139139

src/app/infrastructure/Directions.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Pi2Decimals, radiansToDegrees } from '@app/infrastructure/math/MathUtils'
2+
13
export enum Directions {
24
N = 'N',
35
NE = 'NE',
@@ -14,4 +16,44 @@ export enum MovingDirections {
1416
right = 'right',
1517
down = 'down',
1618
left = 'left',
17-
}
19+
}
20+
21+
const directionsAngleRangesLUT: { [key in Directions]: { min: number, max: number }} = {
22+
[Directions.E ]: { min: 337, max: 22, }, // min & max are degrees
23+
[Directions.SE]: { min: 22, max: 67, },
24+
[Directions.S ]: { min: 67, max: 112, },
25+
[Directions.SW]: { min: 112, max: 157, },
26+
[Directions.W ]: { min: 157, max: 202, },
27+
[Directions.NW]: { min: 202, max: 247, },
28+
[Directions.N ]: { min: 247, max: 292, },
29+
[Directions.NE]: { min: 292, max: 337, },
30+
}
31+
32+
/*
33+
* Angles:
34+
* N
35+
* |
36+
* 270
37+
* W - 180 0 - E
38+
* 90
39+
* |
40+
* S
41+
*/
42+
export function getDirectionBasedOnAngle(theta: number): Directions {
43+
if (theta < 0) {
44+
theta = 2 * Math.PI - Math.abs(theta)
45+
}
46+
theta = radiansToDegrees(theta)
47+
48+
if (
49+
theta >= 0 && theta < directionsAngleRangesLUT[Directions.E].max ||
50+
theta >= directionsAngleRangesLUT[Directions.E].min && theta < 360 // deg
51+
) {
52+
return Directions.E
53+
}
54+
55+
const [ direction ] = Object.entries(directionsAngleRangesLUT).find(([direction, angleRange]) => {
56+
return (theta >= angleRange.min && theta < angleRange.max)
57+
})
58+
return Directions[direction as Directions]
59+
}

src/app/infrastructure/math/MathUtils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ export const Pi2Decimals = +Math.PI.toFixed(2)
22

33
export function random(min: number, max: number) {
44
return min + Math.floor(Math.random() * (max - min + 1))
5+
}
6+
7+
export function radiansToDegrees(radians: number) {
8+
return radians * (180 / Math.PI)
59
}

0 commit comments

Comments
 (0)