-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
130 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const Direction = Object.freeze({ | ||
none: 0, | ||
n: 1, | ||
s: 2, | ||
e: 4, | ||
w: 8, | ||
dirs: ['n', 's', 'e', 'w'], | ||
dx: { e: 1, w: -1, n: 0, s: 0 }, | ||
dy: { e: 0, w: 0, n: -1, s: 1 }, | ||
opposite: { e: 8, w: 4, n: 2, s: 1 } | ||
}) | ||
|
||
export { Direction as default } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import BoundingRect from './bounding_rect' | ||
|
||
function Enemy(spec) { | ||
const s = spec || {} | ||
const graphics = s.graphics | ||
const movementBehavior = s.movementBehavior | ||
const frame = s.frame || BoundingRect(spec) | ||
|
||
const update = (delta) => { | ||
movementBehavior.update(delta) | ||
} | ||
|
||
const render = () => { | ||
graphics.ctx.save() | ||
graphics.ctx.fillStyle = 'red' | ||
graphics.drawCircle(frame.x, frame.y, frame.width / 2) | ||
graphics.ctx.restore() | ||
} | ||
|
||
return { | ||
frame: frame, | ||
update: update, | ||
render: render | ||
} | ||
} | ||
|
||
export { Enemy as default } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Direction from './direction' | ||
import { randomIntInRange } from './utils' | ||
|
||
function RandomMovementBehavior(spec) { | ||
const s = spec || {} | ||
const frame = s.frame | ||
const targetFrame = s.targetFrame | ||
let speed = s.speed || 0.31 | ||
let lastPosition = { x: frame.x, y: frame.y } | ||
let targetVector = { x: targetFrame.centerX(), y: targetFrame.centerY() } | ||
let direction = s.direction || Direction.none | ||
let randomness = s.randomness || 0.04 | ||
let actionDistance = s.actionDistance || 250 | ||
|
||
const update = (delta) => { | ||
targetVector.x = targetFrame.centerX() - frame.centerX() | ||
targetVector.y = targetFrame.centerY() - frame.centerY() | ||
const hypotenuse = Math.hypot(targetVector.x, targetVector.y) | ||
if (hypotenuse < actionDistance) { | ||
targetVector.x /= hypotenuse | ||
targetVector.y /= hypotenuse | ||
frame.x += targetVector.x * speed * delta | ||
frame.y += targetVector.y * speed * delta | ||
} else { | ||
doRandomWalk(delta) | ||
} | ||
|
||
if (frame.x === lastPosition.x && frame.y === lastPosition.y) { | ||
direction = getRandomDirection() | ||
} | ||
lastPosition.x = frame.x | ||
lastPosition.y = frame.y | ||
} | ||
|
||
const doRandomWalk = (delta) => { | ||
if (direction === Direction.none) { | ||
direction = getRandomDirection() | ||
} | ||
|
||
if ((direction & Direction.n) !== 0) { | ||
frame.y -= delta * speed | ||
} else if ((direction & Direction.s) !== 0) { | ||
frame.y += delta * speed | ||
} | ||
if ((direction & Direction.e) !== 0) { | ||
frame.x += delta * speed | ||
} else if ((direction & Direction.w) !== 0) { | ||
frame.x -= delta * speed | ||
} | ||
|
||
if (Math.random() < randomness) { | ||
direction |= getRandomDirection() | ||
} | ||
} | ||
|
||
const getRandomDirection = () => { | ||
return Direction[Direction.dirs[randomIntInRange(0, Direction.dirs.length)]] | ||
} | ||
|
||
return { | ||
update: update | ||
} | ||
} | ||
|
||
export { RandomMovementBehavior } |