forked from excaliburjs/excalibird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bird.js
118 lines (91 loc) · 3.16 KB
/
bird.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var Bird = ex.Actor.extend({
constructor: function(engine){
// call super constructor
var centerX = engine.getWidth()/2;
var centerY = engine.getHeight()/2;
ex.Actor.apply(this, [centerX, centerY, Config.BirdWidth, Config.BirdHeight]);
// initialize acceleration downwards off of global config
this.ay = Config.BirdAccel;
this.anchor = new ex.Point(0.5, 0.6);
// setup animations
var spriteSheet = new ex.SpriteSheet(Resource.BirdSpriteSheet, 4, 1, 32, 32);
this.upAnimation = spriteSheet.getAnimationByIndices(engine, [2, 1, 0], 150);
this.upAnimation.setScaleX(gameScale.x);
this.upAnimation.setScaleY(gameScale.y);
this.upAnimation.freezeFrame = 2;
this.downAnimation = spriteSheet.getAnimationByIndices(engine, [0, 3, 2], 150);
this.downAnimation.setScaleX(gameScale.x);
this.downAnimation.setScaleY(gameScale.y);
this.downAnimation.freezeFrame = 2;
this.addDrawing("up", this.upAnimation);
this.addDrawing("down", this.downAnimation);
this.setCenterDrawing(true);
this.scale.setTo(gameScale.x*Config.BirdScale, gameScale.y*Config.BirdScale);
this.dead = false;
// setup passive collision, meaning it will get collsion events but not be moveds
this.collisionType = ex.CollisionType.Passive;
this.on('collision', function(){
if(!this.dead){
console.log("Collision!")
dispatcher.stop();
if(this.actionQueue.hasNext()){
this.actionQueue.clearActions();
}
this.dead = true;
this.rx = 10;
engine.input.pointers.primary.off("down");
gameOver();
}
//this.moveTo(-1000, 1000, 300).kill();
});
this.on('exitviewport', function(){
if(!this.dead){
console.log("Exit viewport!")
this.dead = true;
this.rx = 10;
dispatcher.stop();
if(this.actionQueue.hasNext()){
this.actionQueue.clearActions();
}
engine.input.pointers.primary.off("down");
gameOver();
}
});
this.animatingUpwards = false;
},
update: function(engine, delta){
// call super update
ex.Actor.prototype.update.apply(this, [engine, delta]);
if(!this.dead){
// if bird is falling play down animation
if(this.dy > 0){
//this.downAnimation.reset();
this.setDrawing("down");
}
// only calculate if not animating to make the snap less jarring
if(!this.animatingUpwards){
// calculate bird's angle
var velocityAngle = new ex.Vector(-Config.LevelSpeed, this.dy).normalize().toAngle();
this.rotation = velocityAngle;
}
// clamp velocity, otherwise we get going too fast
this.dy = ex.Util.clamp(this.dy, -Config.BirdMaxVel, Config.BirdMaxVel);
}
},
bounce: function(){
// applies impulse upward and plays the rewound animation
this.dy = Config.BirdImpulse * gameScale.x;
this.upAnimation.reset();
this.setDrawing("up");
// Play flap sound
Resource.FlapSound.play();
// Apply a smoothing effect to make the snap less jarring
var velocityAngle = new ex.Vector(-Config.LevelSpeed, this.dy).normalize().toAngle();
this.animatingUpwards = true;
var that = this;
// animate a rotation over 150 ms then switch the animating flag
this.rotateBy(velocityAngle, 130).callMethod(function(){
that.animatingUpwards = false;
});
}
})