-
Notifications
You must be signed in to change notification settings - Fork 0
/
introAnimations.js
135 lines (118 loc) · 3.62 KB
/
introAnimations.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
import { FlyingEnemy } from "./enemy.js";
export const introAnimation = (game) => {
// info modal canvas(s)
const upperCanvas = document.getElementById("upperCanvas");
const middleCanvas = document.getElementById("middleCanvas");
const lowerCanvas = document.getElementById("lowerCanvas");
// info modal ctx(s)
const upperCtx = upperCanvas.getContext("2d");
const middleCtx = middleCanvas.getContext("2d");
const lowerCtx = lowerCanvas.getContext("2d");
// player aka dog
const playerImage = player;
const playerwidth = 100;
const playerHeight = 91.3;
const playerSizeModifier = 0.5;
let frameX = 0;
let frameXSit = 0;
let frameY = 3;
let x = 0;
let speedX = Math.random() * 1 + 1;
let y = upperCanvas.height - playerHeight * playerSizeModifier;
// middle canvas plant
const plantEnemy = plant;
const plantWidth = 60;
const plantHeight = 87;
const plantSizeModifier = 1.3;
let frameXPlant = 0;
// lower canvas enemies
let enemyTimer = 0;
let enemySponInterval = 750;
let enemies = [];
// frame update variables
let lastTime = 0;
let fps = 20;
let frameInterval = 1000 / fps;
let frameTimer = 0;
let maxFrames = 8;
function animateIntroCanvas(timestamp) {
const deltaTime = timestamp - lastTime;
lastTime = timestamp;
// animate player
// movement along X axis
x += speedX + game.mode * 0.7;
if (x >= upperCanvas.width) {
x = -playerwidth;
}
// update frames
if (frameTimer > frameInterval) {
frameTimer = 0;
if (frameX >= maxFrames) frameX = 0;
else if (frameXSit >= 4) frameXSit = 0;
else if (frameXPlant >= 1) frameXPlant = 0;
else {
frameX += 1;
frameXSit += 1;
frameXPlant += 1;
}
} else frameTimer += deltaTime;
// add new enemy
if (enemyTimer > enemySponInterval) {
enemyTimer = 0;
const enemyCanvasData = {
speed: Math.random() * 0.5 + 0.1 + game.mode * 0.7,
debug: false,
width: lowerCanvas.width,
height: lowerCanvas.height * 0.5,
gameMargin: 0,
};
let enemySizeModifier = Math.random() * 0.5 + 0.2;
if (window.innerWidth < 430) {
enemySizeModifier = Math.random() * 0.5 + 0.5;
}
enemies.push(new FlyingEnemy(enemyCanvasData, enemySizeModifier));
} else enemyTimer += deltaTime;
enemies.forEach((enemy) => enemy.update(deltaTime));
enemies = enemies.filter((e) => !e.markedForRemoval);
upperCtx.clearRect(0, 0, upperCanvas.width, upperCanvas.height);
middleCtx.clearRect(0, 0, middleCanvas.width, middleCanvas.height);
lowerCtx.clearRect(0, 0, lowerCanvas.width, lowerCanvas.height);
upperCtx.drawImage(
playerImage,
frameX * playerwidth,
frameY * playerHeight,
playerwidth,
playerHeight,
x,
y,
playerwidth * playerSizeModifier,
playerHeight * playerSizeModifier
);
middleCtx.drawImage(
plantEnemy,
frameXPlant * plantWidth,
0,
plantWidth,
plantHeight,
middleCanvas.width * 0.6 - plantWidth * 0.5,
middleCanvas.height - plantHeight * plantSizeModifier,
plantWidth * plantSizeModifier,
plantHeight * plantSizeModifier
);
middleCtx.drawImage(
playerImage,
frameXSit * playerwidth,
5 * playerHeight,
playerwidth,
playerHeight,
middleCanvas.width * 0.4 - playerwidth * 0.5,
middleCanvas.height - playerHeight,
playerwidth,
playerHeight
);
enemies.forEach((enemy) => enemy.draw(lowerCtx));
// animate enemies
requestAnimationFrame(animateIntroCanvas);
}
animateIntroCanvas(0);
};