-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.cpp
More file actions
150 lines (130 loc) · 4.58 KB
/
Copy pathcat.cpp
File metadata and controls
150 lines (130 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "cat.h"
#include <raylib.h>
Cat::Cat(){
getRandomCat();
respawn();
SetSoundVolume(this->explosionSound, 0.10f);
this->frameWidth = this->explosionTexture.width / 16; // Asuming 16 frames
this->frameHeight = this->explosionTexture.height;
this->frameCount = 16; // Number of frames
this->frameSpeed = 0.025f; // Speed (seconds per frame)
this->currentFrame = 0; // Current frame
this->animationTimer = 0; // Animation Timer
}
void Cat::getRandomCat(){
switch (GetRandomValue(1, 8)) {
case 1:
this->catTexture = cat_01;
break;
case 2:
this->catTexture = cat_02;
break;
case 3:
this->catTexture = cat_03;
break;
case 4:
this->catTexture = cat_04;
break;
case 5:
this->catTexture = cat_05;
break;
case 6:
this->catTexture = cat_06;
break;
case 7:
this->catTexture = cat_07;
break;
case 8:
this->catTexture = cat_08;
break;
}
}
void Cat::draw(float deltaTime, GameState& gameState) {
DrawTextureEx(this->catTexture, { this->posX, this->posY }, 0, 1, WHITE);
// Change direction every second
this->timer += deltaTime;
if (this->timer >= 1.0f) {
getNewDirection();
}
if (gameState.shield > 0) {
// Handle collision with shield
if ((this->posX + (float)this->catTexture.width > 800) || this->posX < 0) {
PlaySound(this->shieldBounceSound);
gameState.shield -= 1;
respawn();
getRandomCat();
getNewDirection();
}
if ((this->posY + (float)this->catTexture.height > 450) || this->posY < 0) {
PlaySound(this->shieldBounceSound);
gameState.shield -= 1;
respawn();
getRandomCat();
getNewDirection();
}
} else {
// Handle collision with border
if (this->posX > 800 || (this->posX + (float)this->catTexture.width < 0)) {
gameState.health -= 1;
respawn();
getRandomCat();
getNewDirection();
}
if (this->posY > 450 || (this->posY + (float)this->catTexture.height < 0)) {
gameState.health -= 1;
respawn();
getRandomCat();
getNewDirection();
}
}
// Handle explosion
if (this->isExploding) {
// Only play the sound effect in the first frame
if (this->currentFrame == 0 && this->animationTimer == 0) {
gameState.score += 100;
PlaySound(this->explosionSound);
}
this->animationTimer += deltaTime;
// Update frame based on time
if (this->animationTimer >= this->frameSpeed) {
this->animationTimer = 0;
this->currentFrame = (this->currentFrame + 1) % this->frameCount;
}
Rectangle sourceRect = {
(float)this->currentFrame * this->frameWidth, 0, // X position in spritesheet
(float)this->frameWidth, (float)this->frameHeight // Frame size
};
Rectangle destRect = { this->posX, this->posY, (float)this->frameWidth, (float)this->frameHeight };
DrawTexturePro(this->explosionTexture, sourceRect, destRect, { 0, 0 }, 0, WHITE);
if (currentFrame == 15) {
this->isExploding = false;
currentFrame = 0;
respawn();
getRandomCat();
}
}
}
void Cat::checkForClick(Vector2 crossHairPosition){
// Nuclear mess of an if to read but this aint python lil bro get used to shit awful code
if (((crossHairPosition.x <= (this->posX + (float)this->catTexture.width) && crossHairPosition.x >= this->posX) &&
(crossHairPosition.y <= (this->posY + (float)this->catTexture.height) && crossHairPosition.y >= this->posY))
&& IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
this->isExploding = true;
}
}
void Cat::respawn(){
getNewDirection();
this->posX = (float)GetRandomValue(50, (int)(700-this->catTexture.width));
this->posY = (float)GetRandomValue(50, (int)(350-this->catTexture.height));
}
void Cat::getNewDirection(){
this->timer = 0;
this->movingDiretion.x =(float)GetRandomValue(-2, 2);
this->movingDiretion.y =(float)GetRandomValue(-2, 2);
}
void Cat::move(GameState& gameState){
if(!this->isExploding){
this->posX += this->movingDiretion.x * gameState.catSpeedMult;
this->posY += this->movingDiretion.y * gameState.catSpeedMult;
}
}