BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

fruit border
timfa wsadhelios · pushed by timfa · 4 years ago
5288794a History
frogScene.js 4.5 KiB Download Raw
1class FrogScene extends Scene
2{
3
4 fruitSpawnRateMin = 1;
5 fruitSpawnRateMax = 2;
6 fruitSpawnTimer = 0;
7
8 fruitSpawnChance = .2;
9
10 fruits = [];
11 fruitPool = [];
12 eatenFruit = [];
13
14 gameSpeed = 1;
15
16 constructor (x, y, width, height, displayMode = 0)
17 {
18 super(x, y, width, height, displayMode);
19
20 this.fruitSpawnTimer = this.fruitSpawnRateMax;
21 }
22
23 update (deltaTime)
24 {
25 super.update(deltaTime);
26
27 this.spawnFruits(deltaTime);
28 }
29
30 increaseDifficulty ()
31 {
32 this.gameSpeed += .1;
33
34 if (this.fruitSpawnRateMax > .1)
35 {
36 this.fruitSpawnRateMax -= .2;
37 }
38 else if (this.fruitSpawnRateMax == .2)
39 {
40 this.fruitSpawnRateMax = .1;
41 }
42 if (this.fruitSpawnRateMin > 0)
43 {
44 this.fruitSpawnRateMin -= .2;
45 }
46 }
47
48
49 spawnFruits (deltaTime)
50 {
51 this.fruitSpawnTimer -= deltaTime;
52 if (this.fruitSpawnTimer < this.fruitSpawnRateMin)
53 {
54 if (this.shouldSpawn() || this.fruitSpawnTimer < 0)
55 {
56 let fruit = this.getFromFruitPoolOrCreate();
57 fruit.collider.enabled = true;
58 fruit.speed = Math.randomRange(10, 20);
59 fruit.transform.position = this.getRandomVectorPosition();
60 this.setRandomSubImage(fruit);
61 this.fruits.push(fruit);
62
63 this.fruitSpawnTimer = this.fruitSpawnRateMax;
64 }
65 }
66 }
67
68 consumeFruit (fruit)
69 {
70 let currFruit = { scaleX: .5, scaleY: .5, image: fruit.renderer.subImage };
71 this.eatenFruit.push(currFruit);
72
73 new TWEEN.Tween(currFruit)
74 .to({ scaleX: 1, scaleY: 1 }, 400)
75 .easing(TWEEN.Easing.Elastic.Out)
76 .start();
77
78 this.destroyFruit(fruit);
79
80 this.increaseDifficulty();
81 }
82
83 destroyFruit (fruit)
84 {
85 if (this.fruits.contains(fruit))
86 {
87 this.fruits.remove(fruit);
88 this.fruitPool.push(fruit);
89 fruit.transform.position.x = 50000;
90 }
91 }
92
93 getFromFruitPoolOrCreate ()
94 {
95 return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new Fruit(0, 0, this.game.images.fruit, 0));
96 }
97
98 setRandomSubImage (fruit)
99 {
100 fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages));
101 }
102
103 draw ()
104 {
105 super.draw();
106
107 if (this.eatenFruit.length > 0)
108 {
109 let offX = this.game.images.fruit.width;
110 let offY = this.game.images.fruit.height;
111
112 let startingX = this.real_position.x - offX / 2;
113 let startingY = this.real_position.y - offY / 2;
114
115 let maxPerXAxis = Math.ceil(this.real_size.x / offX) + 1;
116 let xAxisCount = 0;
117
118 let maxPerYAxis = Math.ceil(this.real_size.y / offY) + 1;
119 let yAxisCount = 0;
120
121 let currentPos = new vector(startingX, startingY);
122
123 for (let i = 0; i < this.eatenFruit.length; i++)
124 {
125 //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
126
127 this.game.images.fruit.draw(this.parentScene,
128 currentPos.x, currentPos.y,
129 offX * this.eatenFruit[i].scaleX, offY * this.eatenFruit[i].scaleY,
130 0, 0, 0, 1, this.eatenFruit[i].image);
131
132 console.log(this.eatenFruit[i].scaleX);
133
134 if (xAxisCount < maxPerXAxis)
135 {
136 xAxisCount++;
137 currentPos = currentPos.add(new vector(offX, 0));
138 }
139 else if (yAxisCount < maxPerYAxis)
140 {
141 yAxisCount++;
142 currentPos = currentPos.add(new vector(0, offY));
143 }
144 else if (xAxisCount < maxPerXAxis * 2)
145 {
146 xAxisCount++;
147 currentPos = currentPos.subtract(new vector(offX, 0));
148 }
149 else if (yAxisCount < maxPerYAxis * 2)
150 {
151 yAxisCount++;
152 currentPos = currentPos.subtract(new vector(0, offY));
153 }
154
155
156 }
157 }
158 }
159
160 getRandomVectorPosition ()
161 {
162 return new vector
163 (
164 Math.floor(Math.randomRange(0, this.real_size.x)),
165 0
166 );
167 }
168
169 shouldSpawn ()
170 {
171 return Math.random() < this.fruitSpawnChance;
172 }
173}