-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcap.h
More file actions
145 lines (120 loc) · 3.88 KB
/
Copy pathcap.h
File metadata and controls
145 lines (120 loc) · 3.88 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
#ifndef CAP_H
#define CAP_H
#include "C:\raylib\raylib\src\raylib.h"
#include "settings.h"
Vector2 normalized(Vector2 v) {
if (v.x != 0 && v.y != 0) {
if (v.x < 0) v.x = -1;
else if (v.x > 0) v.x = 1;
if (v.y < 0) v.y = -1;
else if (v.y > 0) v.y = 1;
}
return v;
}
class Cap {
private:
Texture texture;
Vector2 pos;
int speed;
Vector2 direction;
Vector2 size;
int tokens;
int cash;
int energy;
bool gameOver;
float collisionRadius;
int stepsMoved = 0;
const int stepsPerDrain = 25;
const int energyPerDrain = 1;
public:
Cap(Vector2 p={0,0}, int s=CAP_SPEED, Vector2 d={0,0}, Texture t=LoadTexture("assets/customCity/cap0.png")) : texture(t), pos(p), speed(s), direction(d), size({(float)t.width, (float)t.height}), tokens(0), cash(INITIAL_CASH), energy(75), gameOver(false), collisionRadius(size.y/2) {}
void input() {
direction.x = int(IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) - int(IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A));
direction.y = int(IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) - int(IsKeyDown(KEY_UP) || IsKeyDown(KEY_W));
direction = normalized(direction);
}
void constraint() {
pos.x = ((pos.x < (WINDOW_WIDTH - size.x)) ? pos.x : (WINDOW_WIDTH - size.x));
pos.x = (pos.x > 0) ? pos.x : 0;
pos.y = ((pos.y < (WINDOW_HEIGHT - size.y)) ? pos.y : (WINDOW_HEIGHT - size.y));
pos.y = (pos.y > 0) ? pos.y : 0;
}
~Cap() {}
void move(double dt) {
if (energy <= MIN_ENERGY) {
gameOver = true;
return;
}
Vector2 prevPos = pos;
pos.x += direction.x * speed * dt;
pos.y += direction.y * speed * dt;
if ((int)pos.x != (int)prevPos.x || (int)pos.y != (int)prevPos.y) {
stepsMoved++;
if (stepsMoved >= stepsPerDrain) {
decreaseEnergy(energyPerDrain);
stepsMoved = 0;
}
}
}
void update(double dt) {
input();
move(dt);
constraint();
};
void draw() {
DrawTextureV(texture, pos, WHITE);
}
Vector2 getPos() { return pos; }
virtual Vector2 getCenter() {
return Vector2({pos.x + size.x/2, pos.y + size.y/2});
}
float getCollisionRadius() { return collisionRadius; }
void setSize(Vector2 s) {
size = s;
}
Vector2 getSize() {
return size;
}
void setTexture(Texture2D tex) {
texture = tex;
}
void setPosition(Vector2 p) {
pos = p;
}
Rectangle getRectangle() {
return {pos.x,pos.y, size.x, size.y};
}
void addTokens(int amount) {
tokens += amount;
}
void removeTokens(int amount) {
tokens -= amount;
}
void addCash(int amount) {
cash += amount;
}
void removeCash(int amount) {
cash -= amount;
}
void increaseEnergy(int amount) {
if ((energy + amount) < MAX_ENERGY) {
energy += amount;
} else {
energy = MAX_ENERGY;
}
}
void decreaseEnergy(int amount) {
if ((energy - amount) > MIN_ENERGY) {
energy -= amount;
} else {
energy = MIN_ENERGY;
}
}
int getCash() { return cash; }
int getTokens() { return tokens; }
int getEnergy() { return energy; }
bool getGameOver() { return gameOver; }
friend class ATMCity;
friend class Lobby;
};
#endif