Skip to content

Commit

Permalink
Add basic enemy
Browse files Browse the repository at this point in the history
  • Loading branch information
gjeck committed Aug 23, 2017
1 parent 6bc5aff commit b18d8a2
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 17 deletions.
13 changes: 13 additions & 0 deletions src/app/direction.js
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 }
27 changes: 27 additions & 0 deletions src/app/enemy.js
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 }
2 changes: 1 addition & 1 deletion src/app/hero.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function Hero(spec) {
s.height = s.height || 25
const graphics = s.graphics
const inputController = s.gameInputController
let frame = BoundingRect(spec)
let frame = s.rect || BoundingRect(spec)
let speed = s.speed || 0.32

const update = (delta) => {
Expand Down
21 changes: 21 additions & 0 deletions src/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import MazeGenerator from './maze'
import Quadtree from './quadtree'
import { randomIntInRange } from './utils'
import CollisionResolver from './collision_resolver'
import Enemy from './enemy'
import { RandomMovementBehavior } from './movement_behavior'
import BoundingRect from './bounding_rect'

document.addEventListener('DOMContentLoaded', function() {
const canvas = document.getElementById('canvas')
Expand Down Expand Up @@ -39,6 +42,18 @@ document.addEventListener('DOMContentLoaded', function() {
gameInputController: gameInputController
})

const enemyFrame = BoundingRect({ x: 40, y: 120, width: 25, height: 25 })
const movementBehavior = RandomMovementBehavior({
frame: enemyFrame,
targetFrame: hero.frame
})

const enemy = Enemy({
graphics: graphics,
frame: enemyFrame,
movementBehavior: movementBehavior
})

let treePadding = map.tileSize
const quadtree = Quadtree({
x: -treePadding,
Expand All @@ -56,12 +71,17 @@ document.addEventListener('DOMContentLoaded', function() {

emitter.on('RunLoop:update', (delta) => {
hero.update(delta)
enemy.update(delta)

quadtree.insert(hero)
quadtree.insert(enemy)
walls.forEach((wall) => { quadtree.insert(wall) })

let results = quadtree.query(hero.frame)
collisionResolver.resolve(hero.frame, results)

let enemyResults = quadtree.query(enemy.frame)
collisionResolver.resolve(enemy.frame, enemyResults)
})

emitter.on('RunLoop:render', (interpolationPercentage) => {
Expand All @@ -70,6 +90,7 @@ document.addEventListener('DOMContentLoaded', function() {
camera.follow(hero.frame)
map.render(camera.viewport)
hero.render()
enemy.render()
camera.end()
})

Expand Down
2 changes: 1 addition & 1 deletion src/app/map.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Direction } from './maze'
import Direction from './direction'
import BoundingRect from './bounding_rect'
import { randomIntInRange } from './utils'

Expand Down
17 changes: 2 additions & 15 deletions src/app/maze.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { shuffle } from './utils'
import Direction from './direction'

function MazeGenerator() {
const generate = (rows, cols, px, py) => {
Expand Down Expand Up @@ -61,18 +62,4 @@ function MazeGenerator() {
}
}

const Direction = Object.freeze({
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 {
MazeGenerator as default,
Direction
}
export { MazeGenerator as default }
65 changes: 65 additions & 0 deletions src/app/movement_behavior.js
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 }

0 comments on commit b18d8a2

Please sign in to comment.