Last active
November 28, 2016 15:45
-
-
Save raduGaspar/6040d382bba758b57a0201a3974a7f32 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 { Scene, DisplayObject } from '../../engine'; | |
export default class Rock extends DisplayObject { | |
constructor(model) { | |
super(); | |
this.model = model; | |
} | |
update() { | |
if (!this.model.x) { | |
this.model.x = Math.random() * this.scene.width; | |
} | |
if (!this.model.y) { | |
this.model.y = Math.random() * this.scene.height; | |
} | |
this.model.scene = this.scene; | |
this.model.update(); | |
Scene.wrap(this.model); | |
} | |
render() { | |
const { x, y, angle, points, color } = this.model; | |
this.ctx.save(); | |
this.ctx.beginPath(); | |
this.ctx.translate(x, y); | |
this.ctx.rotate(angle); | |
this.ctx.lineWidth = 2; | |
this.ctx.strokeStyle = '#111'; | |
this.ctx.fillStyle = color; | |
for(let i=0; i<points.length; i++) { | |
if(i === 0) { | |
this.ctx.moveTo(points[i].x, points[i].y); | |
} else { | |
this.ctx.lineTo(points[i].x, points[i].y); | |
} | |
} | |
this.ctx.fill(); | |
this.ctx.stroke(); | |
this.ctx.closePath(); | |
this.ctx.restore(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment