forked from JoshuaKGoldberg/Old-Deleted-FullScreenMario
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upkeep.js
195 lines (168 loc) · 5.46 KB
/
upkeep.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
185
186
187
188
189
190
191
192
193
194
195
/* Upkeep.js */
// Contains functions associated with the upkeep
function upkeep() {
if(window.paused) return;
// window.nextupk = requestAnimationFrame(upkeep);
window.nextupk = setTimeout(upkeep, timer);
// Adjust for differences in performance
adjustFPS();
// Quadrants upkeep
determineAllQuadrants();
// Solids upkeep
maintainSolids();
// Character upkeep
maintainCharacters();
// Mario specific
maintainMario();
// Texts upkeep, if there are any
if(texts.length) maintainTexts();
// Events upkeep
handleEvents();
refillCanvas();
}
function adjustFPS() {
window.time_now = now();
var time_diff = time_now - time_prev,
fps_actual = roundDigit(1000 / time_diff, .001);
window.fps = roundDigit((.7 * fps) + (.3 * fps_actual), .01);
window.realtime = fps_target / fps;
window.time_prev = time_now;
}
function pause(big) {
if(paused && !window.nextupk) return;
cancelAnimationFrame(nextupk);
pauseAllSounds();
paused = true;
if(big) play("Pause.wav");
}
function unpause() {
if(!paused) return;
window.nextupk = requestAnimationFrame(upkeep);
paused = false;
resumeAllSounds();
}
// Solids by themselves don't really do much
function maintainSolids(update) {
for(var i = 0, solid; i < solids.length; ++i) {
solid = solids[i];
if(solid.alive) {
if(solid.movement) solid.movement(solid);
}
if(!solid.alive || solid.right < quads.delx)
deleteThing(solid, solids, i);
}
}
function maintainCharacters(update) {
var delx = gamescreen.right + quads.rightdiff,
character, i;
for(i = 0; i < characters.length; ++i) {
character = characters[i];
// Gravity
if(!character.resting) {
if(!character.nofall) character.yvel += character.gravity || map.gravity;
character.yvel = min(character.yvel, map.maxyvel);
} else character.yvel = 0;
// Position updating and collision detection
updatePosition(character);
determineThingQuadrants(character);
character.under = character.undermid = false;
determineThingCollisions(character);
// Resting tests
if(character.resting) {
if(!characterOnResting(character, character.resting)) {
character.resting = false; // Necessary for moving platforms :(
} else {
/*character.jumping = */character.yvel = false;
setBottom(character, character.resting.top);
}
}
// Movement or deletion
// To do: rethink this...
//// Good for performance if gamescreen.bottom - gamescreen.top is saved in screen and updated on shift
// To do: is map.shifting needed?
if(character.alive) {
if(character.type != "mario" && !map.shifting &&
(character.numquads == 0 || character.left > delx) && !character.outerok) {
// (character.top > gamescreen.bottom - gamescreen.top || character.left < + quads.width * -1)) {
deleteThing(character, characters, i);
}
else {
if(!character.nomove && character.movement)
character.movement(character);
// if(update) updateDisplay(character);
}
}
else if(!map.shifting) deleteThing(character, characters, i);
}
}
function maintainMario(update) {
if(!mario.alive) return;
// Mario is falling
if(mario.yvel > 0) {
if(!map.underwater) mario.keys.jump = 0;
// Jumping?
if(!mario.jumping) {
// Paddling? (from falling off a solid)
if(map.underwater) {
if(!mario.paddling) {
switchClass(mario, "paddling", "paddling");
mario.padding = true;
}
}
else {
addClass(mario, "jumping");
mario.jumping = true;
}
}
// Mario has fallen too far
if(!mario.piping && !mario.dying && mario.top > gamescreen.deathheight) {
// If the map has an exit loc (cloud world), transport there
if(map.exitloc) {
// Random maps will pretend he died
if(map.random) {
goToTransport(["Random", "Overworld", "Down"]);
marioDropsIn();
return;
}
// Otherwise just shift to the location
return shiftToLocation(map.exitloc);
}
// Otherwise, since Mario is below the gamescreen, kill him dead
clearMarioStats();
killMario(mario, 2);
}
}
// Mario is moving to the right
if(mario.xvel > 0) {
if(mario.right > gamescreen.middlex) {
// If Mario is to the right of the gamescreen's middle, move the gamescreen
if(mario.right > gamescreen.right - gamescreen.left)
mario.xvel = min(0, mario.xvel);
}
}
// Mario is moving to the left
else if(mario.left < 0) {
// Stop Mario from going to the left.
mario.xvel = max(0, mario.xvel);
}
// Mario is hitting something (stop jumping)
if(mario.under) mario.jumpcount = 0;
// Scrolloffset is how far over the middle mario's right is
// It's multiplied by 0 or 1 for map.canscroll
window.scrolloffset = (map.canscroll/* || (map.random && !map.noscroll)*/) * (mario.right - gamescreen.middlex);
if(scrolloffset > 0 && !map.shifting) {
scrollWindow(lastscroll = round(min(mario.scrollspeed, scrolloffset)));
}
else lastscroll = 0;
}
// Deletion checking is done by an interval set in shiftToLocation
// This simply does velocity
function maintainTexts() {
var element, me, i;
for(i = texts.length - 1; i >= 0; --i) {
me = texts[i];
element = me.element || me;
if(me.xvel) elementShiftLeft(element, me.xvel);
if(me.yvel) elementShiftTop(element, me.yvel);
}
}