BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

remove line
timfa wsadhelios · pushed by timfa · 4 years ago
a0e57036 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.collider.enabled = true;
39 fruit.transform.position = this.getRandomVectorPosition(fruit);
40 this.setRandomSubImage(fruit);
41 this.fruits.push(fruit);
42
43 this.fruitSpawnTimer = this.fruitSpawnRateMax;
44 }
45 }
46 }
47
48 destroyFruit (fruit)
49 {
50 if (this.fruits.contains(fruit))
51 {
52 this.fruits.remove(fruit);
53 this.fruitPool.push(fruit);
54 fruit.transform.position.x = 50000;
55 }
56 }
57
58 getFromFruitPoolOrCreate ()
59 {
60 return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new Fruit(0, 0, this.game.images.fruit, 0));
61 }
62
63 setRandomSubImage (fruit)
64 {
65 fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages));
66 }
67
68 getRandomVectorPosition (fruit)
69 {
70 return new vector
71 (
72 Math.floor(Math.randomRange(0, this.real_size.x)),
73 0
74 );
75 }
76
77 shouldSpawn ()
78 {
79 return this.fruits.length < this.fruitMax && Math.random() < this.fruitSpawnChance;
80 }
81}