UNPKG

core2d

Version:

Multiplatform 2D interaction engine

45 lines (36 loc) 880 B
"use strict"; import { Command } from "../Command.mjs"; import { Core2D } from "../Core2D.mjs"; import { Sprite } from "../Sprite.mjs"; export class ControllableSprite extends Sprite { constructor() { super(); this.controller = Core2D.getController(); this.step = 2; } setStep(step) { this.step = step; return this; } sync() { if (this.controller.keyDown(Command.LEFT) && this.left > 0) { this.x -= this.step; } if ( this.controller.keyDown(Command.RIGHT) && this.right < this.scene.width ) { this.x += this.step; } if (this.controller.keyDown(Command.UP) && this.top > 0) { this.y -= this.step; } if ( this.controller.keyDown(Command.DOWN) && this.bottom < this.scene.height ) { this.y += this.step; } return Sprite.prototype.sync.call(this); } }