Created
June 18, 2020 17:09
-
-
Save bouveng/83fcc9cb871ea182e0bf94f0b0778acd to your computer and use it in GitHub Desktop.
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
import Entity from '../Entity.js'; | |
import Trait from '../Trait.js'; | |
import Killable from '../traits/Killable.js'; | |
import {loadSpriteSheet} from '../loaders/sprite.js'; | |
export function loadCheepSlow() { | |
return loadSpriteSheet('cheep-gray') | |
.then(createCheepSlowFactory); | |
} | |
export function loadCheepFast() { | |
return loadSpriteSheet('cheep-red') | |
.then(createCheepFastFactory); | |
} | |
export function loadCheepWavy() { | |
return loadSpriteSheet('cheep-gray') | |
.then(createCheepWavyFactory); | |
} | |
class Behavior extends Trait { | |
collides(us, them) { | |
if(them.traits.has(Killable)) { | |
them.traits.get(Killable).kill(); | |
} | |
} | |
update(entity, gameContext, level) { | |
const {deltaTime} = gameContext; | |
entity.pos.x += entity.vel.x * deltaTime; | |
} | |
} | |
class Wavy extends Trait { | |
constructor() { | |
super(); | |
this.amplitude = 32; | |
this.direction = 1; | |
this.offset = 0; | |
} | |
update(entity, gameContext, level) { | |
const {deltaTime} = gameContext; | |
const movementX = entity.vel.x * deltaTime; | |
entity.pos.x += movementX; | |
const movementY = (movementX * this.direction) * 0.5; | |
entity.pos.y += movementY; | |
this.offset += movementY; | |
if (Math.abs(this.offset) > this.amplitude) { | |
this.direction = -this.direction; | |
} | |
} | |
} | |
function createCheepSlowFactory(sprite) { | |
const swimAnim = sprite.animations.get('swim'); | |
function routeAnim(entity) { | |
return swimAnim(entity.lifetime); | |
} | |
function drawCheepSlow(context) { | |
sprite.draw(routeAnim(this), context, 0, 0, true); | |
} | |
return function createCheepSlow() { | |
const entity = new Entity(); | |
entity.size.set(16, 16); | |
entity.vel.x = -16; | |
entity.addTrait(new Behavior()); | |
entity.draw = drawCheepSlow; | |
return entity; | |
}; | |
} | |
function createCheepFastFactory(sprite) { | |
const swimAnim = sprite.animations.get('swim'); | |
function routeAnim(entity) { | |
return swimAnim(entity.lifetime); | |
} | |
function drawCheepFast(context) { | |
sprite.draw(routeAnim(this), context, 0, 0, true); | |
} | |
return function createCheepFast() { | |
const entity = new Entity(); | |
entity.size.set(16, 16); | |
entity.vel.x = -32; | |
entity.addTrait(new Behavior()); | |
entity.draw = drawCheepFast; | |
return entity; | |
}; | |
} | |
function createCheepWavyFactory(sprite) { | |
const swimAnim = sprite.animations.get('swim'); | |
function routeAnim(entity) { | |
return swimAnim(entity.lifetime); | |
} | |
function drawCheepWavy(context) { | |
sprite.draw(routeAnim(this), context, 0, 0, true); | |
} | |
return function createCheepWavy() { | |
const entity = new Entity(); | |
entity.size.set(16, 16); | |
entity.vel.x = -16; | |
entity.addTrait(new Wavy()); | |
entity.draw = drawCheepWavy; | |
return entity; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment