-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmario.js
184 lines (158 loc) · 4.35 KB
/
mario.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*********************************************************************
* Mario
*/
function Mario(canvas, sprite) {
this.canvas = canvas;
this.canvasCtx = canvas.getContext('2d');
this.sprite = sprite;
this.xPos = 50;
this.yPos = 0;
// Position when on the ground.
this.groundYPos = 0;
this.currentFrame = 0;
this.currentAnimFrames = [];
this.blinkDelay = 0;
this.animStartTime = 0;
this.timer = 0;
this.msPerFrame = 1000 / FPS;
this.config = Mario.config;
// Current status.
this.status = Mario.status.WAITING;
this.jumping = false;
this.dying = false; //TODO: player down animation
this.dead = false;
this.jumpVelocity = 0;
this.reachedMinHeight = false;
this.speedDrop = false;
this.jumpCount = 0;
this.jumpspotX = 0;
this.init();
};
Mario.config = {
DROP_VELOCITY: -5,
GRAVITY: 0.6,
HEIGHT: 40,
HEIGHT_DUCK: 25,
INIITAL_JUMP_VELOCITY: -10,
INTRO_DURATION: 1500,
MAX_JUMP_HEIGHT: 30,
MIN_JUMP_HEIGHT: 30,
START_X_POS: 0,
WIDTH: 40,
WIDTH_DUCK: 59
};
Mario.collisionBoxes = [new CollisionBox(0, 0, 20, 20) ];
Mario.status = {
CRASHED: 'CRASHED',
JUMPING: 'JUMPING',
RUNNING: 'RUNNING',
WAITING: 'WAITING'
};
Mario.animFrames = {
WAITING: {
frames: [320],
frameY: 115,
msPerFrame: 1000 / 3
},
RUNNING: {
frames: [320, 360],
frameY: 75,
msPerFrame: 1000 / 12
},
CRASHED: {
frames: [0],
frameY: 35,
msPerFrame: 1000 / 60
},
JUMPING: {
frames: [200],//,240],
frameY: 110,
msPerFrame: 1000 / 60
}
};
Mario.prototype = {
init: function() {
this.groundYPos = Game.defaultDimensions.HEIGHT - this.config.HEIGHT - Game.config.BOTTOM_PAD;
this.yPos = this.groundYPos;
//this.draw(Mario.animFrames.WAITING.framesX[0], Mario.animFrames.WAITING.framesY[0]);
this.update(0, Mario.status.WAITING);
},
update: function(deltaTime, opt_status) {
this.timer += deltaTime;
// Update the status.
if (opt_status) {
this.status = opt_status;
this.currentFrame = 0;
this.msPerFrame = Mario.animFrames[opt_status].msPerFrame;
this.currentAnimFrames = Mario.animFrames[opt_status].frames;
this.currentAnimFramesY = Mario.animFrames[opt_status].frameY;
}
this.draw(this.currentAnimFrames[this.currentFrame], this.currentAnimFramesY);
// Update the frame position.
if (this.timer >= this.msPerFrame) {
this.currentFrame = this.currentFrame == this.currentAnimFrames.length - 1 ? 0 : this.currentFrame + 1;
this.timer = 0;
}
},
draw: function(x, y) {
var sourceX = x;
var sourceY = y;
var sourceWidth = this.config.WIDTH;
var sourceHeight = this.config.HEIGHT;
// Standing / running
this.canvasCtx.drawImage(this.sprite, sourceX, sourceY, sourceWidth, sourceHeight, this.xPos, this.yPos, this.config.WIDTH, this.config.HEIGHT);
},
setJumpVelocity: function(setting) {
this.config.INIITAL_JUMP_VELOCITY = -setting;
this.config.DROP_VELOCITY = -setting / 2;
},
startJump: function(speed) {
if (!this.jumping) {
this.update(0, Mario.status.JUMPING);
this.jumpVelocity = this.config.INIITAL_JUMP_VELOCITY - (speed / 10);
this.jumping = true;
this.reachedMinHeight = false;
this.speedDrop = false;
}
},
endJump: function() {
if (this.reachedMinHeight &&
this.jumpVelocity < this.config.DROP_VELOCITY) {
this.jumpVelocity = this.config.DROP_VELOCITY;
}
},
updateJump: function(deltaTime, speed) {
var msPerFrame = Mario.animFrames[this.status].msPerFrame;
var framesElapsed = deltaTime / msPerFrame;
if(this.dead) return;
if (this.speedDrop) {
this.yPos += Math.round(this.jumpVelocity *
this.config.SPEED_DROP_COEFFICIENT * framesElapsed);
} else {
this.yPos += Math.round(this.jumpVelocity * framesElapsed);
}
this.jumpVelocity += this.config.GRAVITY * framesElapsed;
if (this.yPos < this.minJumpHeight) {
this.reachedMinHeight = true;
}
if (this.yPos < this.config.MAX_JUMP_HEIGHT) {
this.endJump();
}
// Jump is done! =)
if (this.yPos > this.groundYPos) {
this.reset();
this.jumpCount++;
}
this.update(deltaTime);
},
reset: function() {
this.yPos = this.groundYPos;
this.jumpVelocity = 0;
this.jumping = false;
this.ducking = false;
this.update(0, Mario.status.RUNNING);
this.midair = false;
this.speedDrop = false;
this.jumpCount = 0;
}
};