4
4
#include <rlgl.h>
5
5
#include <stdlib.h>
6
6
7
- // Creates a new chappal at a random location with a random rotation speed.
8
7
// Speed will increase with time.
9
-
10
8
static float speed = 6.0f ;
11
9
10
+ // Creates a new chappal at a random location with a random rotation speed.
12
11
Chappal * CreateChappal (Texture2D textures [], Vector2 target ) {
13
- // Texture2D textures
14
12
Chappal * chappal = malloc (sizeof (Chappal ));
13
+
15
14
int x = 0 ;
16
15
int y = 0 ;
17
- const int WIDTH = GetScreenWidth ();
18
- const int HEIGHT = GetScreenHeight ();
16
+
17
+ const int halfWidth = GetScreenWidth () / 2 ;
18
+ const int halfHeight = GetScreenHeight () / 2 ;
19
+
19
20
if (GetRandomValue (0 , 1 )) {
20
21
x = GetRandomValue (
21
- target .x - ( WIDTH / 2 ) - SPAWN_OFFSET ,
22
- target .x + ( WIDTH / 2 ) + SPAWN_OFFSET );
22
+ target .x - halfWidth - SPAWN_OFFSET ,
23
+ target .x + halfWidth + SPAWN_OFFSET );
23
24
if (GetRandomValue (0 , 1 )) {
24
- y = target .y + ( HEIGHT / 2 ) + SPAWN_OFFSET ;
25
+ y = target .y + halfHeight + SPAWN_OFFSET ;
25
26
} else {
26
- y = target .y - ( HEIGHT / 2 ) - SPAWN_OFFSET ;
27
+ y = target .y - halfHeight - SPAWN_OFFSET ;
27
28
}
28
29
} else {
29
30
y = GetRandomValue (
30
- target .y - ( HEIGHT / 2 ) - SPAWN_OFFSET ,
31
- target .y + ( HEIGHT / 2 ) + SPAWN_OFFSET );
31
+ target .y - halfHeight - SPAWN_OFFSET ,
32
+ target .y + halfHeight + SPAWN_OFFSET );
32
33
if (GetRandomValue (0 , 1 )) {
33
- x = target .x + ( WIDTH / 2 ) + SPAWN_OFFSET ;
34
+ x = target .x + halfWidth + SPAWN_OFFSET ;
34
35
} else {
35
- x = target .x - ( WIDTH / 2 ) - SPAWN_OFFSET ;
36
+ x = target .x - halfWidth - SPAWN_OFFSET ;
36
37
}
37
38
}
39
+
38
40
Vector2 position = {x , y };
39
41
chappal -> position = position ;
40
42
Vector2 direction = Vector2Subtract (target , chappal -> position );
41
43
direction = Vector2Normalize (direction );
42
44
chappal -> rotation = 0.0f ;
43
45
chappal -> rotationSpeed = (float )GetRandomValue (ROTATION_SPEED_MIN , ROTATION_SPEED_MAX ) / 10.0f ;
44
46
chappal -> direction = direction ;
45
- // chappal->texture = texture;
46
- // chappal->type = random type between 1 to n
47
-
48
- // int randNum = rng_u64(rng_u64(time(NULL))) % 100;
49
47
50
- int randNum = GetRandomValue (1 , 100 );
48
+ // Choose a weapon randomly.
49
+ int weaponChoice = GetRandomValue (1 , 100 );
51
50
52
- if (randNum < 5 )
51
+ if (weaponChoice < 5 )
53
52
chappal -> type = KHANA ;
54
- else if (randNum < 20 )
53
+ else if (weaponChoice < 20 )
55
54
chappal -> type = DANDA ;
56
- else if (randNum < 40 )
55
+ else if (weaponChoice < 40 )
57
56
chappal -> type = JUTA ;
58
57
else
59
58
chappal -> type = CHAPPAL ;
59
+
60
+ // Set weapon texture.
60
61
chappal -> texture = textures [chappal -> type ];
62
+
61
63
return chappal ;
62
64
}
63
65
64
66
void UpdateChappal (Chappal * chappal ) {
65
67
Vector2 velocity = Vector2Scale (chappal -> direction , speed );
66
68
chappal -> position = Vector2Add (chappal -> position , velocity );
67
69
chappal -> rotation += chappal -> rotationSpeed ;
68
- };
70
+ }
69
71
70
72
void DrawChappal (Chappal * chappal ) {
71
73
DrawTexturePro (
@@ -79,16 +81,16 @@ void DrawChappal(Chappal* chappal) {
79
81
(Vector2 ){(float )chappal -> texture .width / 2 , (float )chappal -> texture .height / 2 },
80
82
chappal -> rotation ,
81
83
WHITE );
82
- };
84
+ }
83
85
84
86
void IncreaseSpeed () {
85
87
speed += 4.0f ;
86
- };
88
+ }
87
89
88
90
void SetSpeed (float newSpeed ) {
89
91
speed = newSpeed ;
90
- };
92
+ }
91
93
92
94
void DestroyChappal (Chappal * chappal ) {
93
95
free (chappal );
94
- };
96
+ }
0 commit comments