BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

bip
timfa Tim · pushed by timfa · 4 years ago
a10500f9 History
frogScene.js 1.9 KiB Download Raw
1class FrogScene extends Scene
2{
3 fruitMax = 10;
4
5 fruitSpawnRateMin = 1;
6 fruitSpawnRateMax = 2;
7 fruitSpawnTimer = 0;
8
9 fruitSpawnChance = .2;
10
11 fruits = [];
12 fruitPool = [];
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 spawnFruits (deltaTime)
31 {
32 this.fruitSpawnTimer -= deltaTime;
33 if (this.fruitSpawnTimer < this.fruitSpawnRateMin)
34 {
35 if (this.shouldSpawn() || this.fruitSpawnTimer < 0)
36 {
37 let fruit = this.getFromFruitPoolOrCreate();
38 fruit.transform.position = this.getRandomVectorPosition(fruit);
39 this.setRandomSubImage(fruit);
40 this.fruits.push(fruit);
41
42 this.fruitSpawnTimer = this.fruitSpawnRateMax;
43 }
44 }
45 }
46
47 destroyFruit (fruit)
48 {
49 if (this.fruits.contains(fruit))
50 {
51 this.fruits.remove(fruit);
52 this.fruitPool.push(fruit);
53 fruit.transform.position.x = 50000;
54 }
55 }
56
57 getFromFruitPoolOrCreate ()
58 {
59 return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new Fruit(0, 0, this.game.images.fruit, 0));
60 }
61
62 setRandomSubImage (fruit)
63 {
64 fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages));
65 }
66
67 getRandomVectorPosition (fruit)
68 {
69 return new vector
70 (
71 Math.floor(Math.randomRange(0, this.real_size.x)),
72 0
73 );
74 }
75
76 shouldSpawn ()
77 {
78 return this.fruits.length < this.fruitMax && Math.random() < this.fruitSpawnChance;
79 }
80}