This repository has been archived by the owner on Dec 27, 2018. It is now read-only.
/
collectable.js
71 lines (60 loc) · 1.66 KB
/
collectable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var inherits = require('inherits')
, pointer = require('./pointer')
, chaser = require('./chaser')
, Entity = require('./entity')
, CIRCLE = Math.PI * 2
, counts = 1
, game
module.exports = Collectable
var sprite = new Image
sprite.src = 'ring.png'
function Collectable() {
if (!(this instanceof Collectable)) return new Collectable()
Entity.call(this)
if (!game) throw new Error('game not ready')
this.radius =
this._radius = 150
var angle = Math.random()*CIRCLE
game.boids.attractors.push(
this.pos = [
Math.sin(angle) * (counts * 250 + 800)
, Math.cos(angle) * (counts * 250 + 800)
, this._radius
, -20
]
)
game.manager.add(this.pointer = pointer(this.pos))
counts += 1
}
inherits(Collectable, Entity)
Collectable.register = function(g) {
game = g
}
Collectable.prototype.tick = function() {
this.pos[2] = this._radius = this._radius + (this.radius - this._radius) * 0.1
}
Collectable.prototype.render = function(ctx, manager) {
var camera = manager.first('camera')
, radius = this._radius / 150
ctx.save()
ctx.translate(this.pos[0], this.pos[1])
ctx.scale(radius, radius)
ctx.translate(-150, -150)
ctx.drawImage(sprite, 0, 0)
ctx.restore()
}
Collectable.prototype.doAction = function() {
var sky = game.manager.first('sky')
this.pointer.enabled = false
this.radius = 0
sky.moment = Math.max(sky.moment - 0.1, 0)
game.player.collected += 1
game.collected += 1
game.roundCollected = true
game.sounds.play('point', { volume: 100 })
game.manager.add(chaser(game.player.pos.slice(0)))
}
Collectable.prototype.revive = function() {
this.pointer.enabled = true
this.radius = 150
}