BlueGit

blue-star speeltuin

Files

speeltuin / src / minigame / frog / frogScene.js

frogScene.js

stuff
timfa wsadhelios · pushed by timfa · 4 years ago
74a5c5f9 History
frogScene.js 2.0 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 onLoad()
24 {
25 while(this.fruitPool.length < this.fruitMax)
26 {
27 let fruit = new Fruit(0,0, this.game.images.fruit, 0);
28 this.fruitPool.push(fruit);
29 }
30 }
31
32 update(deltaTime)
33 {
34 super.update(deltaTime);
35
36 this.spawnFruits(deltaTime);
37 }
38
39 spawnFruits(deltaTime)
40 {
41 this.fruitSpawnTimer -= deltaTime;
42 if(this.fruitSpawnTimer < this.fruitSpawnRateMin)
43 {
44 if(this.shouldSpawn() || this.fruitSpawnTimer < 0)
45 {
46 let fruit = this.getFromFruitPoolOrCreate();
47 fruit.destroyed = false;
48 fruit.transform.position = this.getRandomVectorPosition(fruit);
49 this.setRandomSubImage(fruit);
50 this.addObject(fruit);
51 this.fruits.push(fruit);
52
53 this.fruitSpawnTimer = this.fruitSpawnRateMax;
54 }
55 }
56 }
57
58 destroyFruit(fruit)
59 {
60 this.fruits.remove(fruit);
61 this.fruitPool.push(fruit);
62 fruit.destroy();
63 }
64
65 getFromFruitPoolOrCreate()
66 {
67 return this.fruitPool.length > 0 ? this.fruitPool.pop() : new Fruit(0,0, this.game.images.fruit, 0);
68 }
69
70 setRandomSubImage(fruit)
71 {
72 fruit.renderer.subImage = Math.floor(Math.randomRange(0,fruit.renderer.sprite.subImages));
73 }
74
75 getRandomVectorPosition(fruit)
76 {
77 return new vector
78 (
79 Math.floor(Math.randomRange(0, this.real_size.x)),
80 0
81 );
82 }
83
84 shouldSpawn()
85 {
86 return this.fruits.length < this.fruitMax && Math.random() < this.fruitSpawnChance;
87 }
88}