Skip to content

Commit

Permalink
Add dash ability for hero
Browse files Browse the repository at this point in the history
  • Loading branch information
gjeck committed Sep 9, 2017
1 parent 3aad6ed commit fcfc31f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 18 deletions.
16 changes: 13 additions & 3 deletions src/app/collision_resolver.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { MetaType, MetaStatus } from './meta'

function createCollisionResolver() {
const shouldIgnoreResolve = (entityMeta, itemMeta) => {
return (entityMeta.type === MetaType.hero && itemMeta.type === MetaType.arrow) ||
(entityMeta.type === MetaType.hero &&
itemMeta.type === MetaType.enemy &&
(entityMeta.status & MetaStatus.invulnerable) !== 0) ||
(entityMeta.type === MetaType.enemy &&
itemMeta.type === MetaType.hero &&
(itemMeta.status & MetaStatus.invulnerable) !== 0) ||
(itemMeta.status & MetaStatus.active) === 0 ||
(entityMeta.status & MetaStatus.active) === 0
}

const resolve = (entity, collection) => {
collection.forEach((item) => {
if (entity === item || item.meta.type === MetaType.none) {
return
}
const ignoreResolve = (entity.meta.type === MetaType.hero && item.meta.type === MetaType.arrow) ||
(item.meta.status & MetaStatus.active) === 0 ||
(entity.meta.status & MetaStatus.active) === 0
const ignoreResolve = shouldIgnoreResolve(entity.meta, item.meta)
const bottomCollision = entity.frame.maxY() - item.frame.y
const topCollision = item.frame.maxY() - entity.frame.y
const leftCollision = item.frame.maxX() - entity.frame.x
Expand Down
14 changes: 12 additions & 2 deletions src/app/game_input_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ function createGameInputController(spec) {
return keyboard[KeyMapping.down] || false
}

const isDash = () => {
return keyboard[KeyMapping.dash] || false
}

docWindow.addEventListener('keyup', (e) => {
keyboard[e.code] = false
})

docWindow.addEventListener('keydown', (e) => {
keyboard[e.code] = true
if (e.code === KeyMapping.dash && e.target === graphics.canvas) {
e.preventDefault()
}
emitter.emit('GameInputController:keydown', e)
})

graphics.canvas.addEventListener('mousemove', (e) => {
Expand All @@ -46,7 +54,7 @@ function createGameInputController(spec) {
graphics.canvas.addEventListener('mousedown', (e) => {
setMousePoint(e.clientX, e.clientY)
mouse.down = true
emitter.emit('createGameInputController:mousedown', e)
emitter.emit('GameInputController:mousedown', e)
})

graphics.canvas.addEventListener('mouseup', (e) => {
Expand All @@ -59,6 +67,7 @@ function createGameInputController(spec) {
isLeft: isLeft,
isDown: isDown,
isRight: isRight,
isDash: isDash,
mouse: mouse
}
}
Expand All @@ -67,7 +76,8 @@ const KeyMapping = Object.freeze({
up: 'KeyW',
left: 'KeyA',
down: 'KeyS',
right: 'KeyD'
right: 'KeyD',
dash: 'Space'
})

export { createGameInputController as default }
49 changes: 43 additions & 6 deletions src/app/hero.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import createBoundingRect from './bounding_rect'
import { createMeta, MetaType } from './meta'
import { createMeta, MetaType, MetaStatus } from './meta'

function createHero(spec) {
const s = spec || {}
Expand All @@ -8,26 +8,63 @@ function createHero(spec) {
s.type = s.type || MetaType.hero
const graphics = s.graphics
const inputController = s.gameInputController
const emitter = s.emitter
const meta = s.meta || createMeta(s)
const frame = s.rect || createBoundingRect(s)
let speed = s.speed || 0.32
const speed = s.speed || 0.32
const dashResetTimeout = s.dashTimeout || 3000
const dashTimeout = s.dashLength || 500
let dashResetTimerId = null
let isDashing = false
let alphaResetTimerId = null
let alpha = 1.0

emitter.on('GameInputController:keydown', (e) => {
if (dashResetTimerId || !inputController.isDash()) {
return
}
meta.status |= MetaStatus.invulnerable
isDashing = true
setTimeout(() => {
isDashing = false
meta.status &= ~MetaStatus.invulnerable
}, dashTimeout)
dashResetTimerId = setTimeout(() => {
dashResetTimerId = null
}, dashResetTimeout)
})

const getAlpha = () => {
if (isDashing && !alphaResetTimerId) {
alpha = 0.1
alphaResetTimerId = setTimeout(() => {
alpha = alphaResetTimerId % 2 === 0 ? 1.0 : 0.1
alphaResetTimerId = null
}, 100)
} else {
alpha = 1.0
}
return alpha
}

const update = (delta) => {
const newSpeed = isDashing ? speed * 2 : speed
if (inputController.isUp()) {
frame.y -= delta * speed
frame.y -= delta * newSpeed
} else if (inputController.isDown()) {
frame.y += delta * speed
frame.y += delta * newSpeed
}
if (inputController.isRight()) {
frame.x += delta * speed
frame.x += delta * newSpeed
} else if (inputController.isLeft()) {
frame.x -= delta * speed
frame.x -= delta * newSpeed
}
}

const render = () => {
graphics.ctx.save()
graphics.ctx.fillStyle = 'white'
graphics.ctx.globalAlpha = getAlpha()
graphics.drawCircle(frame.x, frame.y, frame.width / 2)
graphics.ctx.restore()
}
Expand Down
4 changes: 1 addition & 3 deletions src/app/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ function createLight(spec) {
const sDx = segment.x2 - segment.x1
const sDy = segment.y2 - segment.y1

const epsilon = 0.0001

if (rDx * sDy === rDy * sDx) { // parallel
return null
}
Expand All @@ -28,7 +26,7 @@ function createLight(spec) {
}

const t2 = (rDx * (sPy - rPy) + rDy * (rPx - sPx)) / denom
const t1 = rDx < epsilon ? (sPy + sDy * t2 - rPy) / rDy : (sPx + sDx * t2 - rPx) / rDx
const t1 = rDx === 0 ? (sPy + sDy * t2 - rPy) / rDy : (sPx + sDx * t2 - rPx) / rDx

if (t1 < 0 || t2 < 0 || t2 > 1) {
return null
Expand Down
5 changes: 3 additions & 2 deletions src/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ document.addEventListener('DOMContentLoaded', function() {
x: 20,
y: 20,
health: 100,
gameInputController: gameInputController
gameInputController: gameInputController,
emitter: emitter
})

const enemies = []
Expand Down Expand Up @@ -142,7 +143,7 @@ document.addEventListener('DOMContentLoaded', function() {
quadtree.removeAll()
})

emitter.on('createGameInputController:mousedown', (e) => {
emitter.on('GameInputController:mousedown', (e) => {
// canvas.classList.toggle('shake')
// canvas.addEventListener('animationend', () => {
// canvas.classList.toggle('shake')
Expand Down
5 changes: 3 additions & 2 deletions src/app/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const MetaType = Object.freeze({
})

const MetaStatus = Object.freeze({
active: 1,
visible: 2
active: 2,
visible: 4,
invulnerable: 8
})

function createMeta(spec) {
Expand Down

0 comments on commit fcfc31f

Please sign in to comment.