-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom_cyberbasic.cpp
More file actions
338 lines (287 loc) · 9.76 KB
/
custom_cyberbasic.cpp
File metadata and controls
338 lines (287 loc) · 9.76 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "raylib.h"
#include "bas/custom_cyberbasic.hpp"
#include <algorithm>
#include <chrono>
#include <cstring>
namespace bas {
// Global storage for sprites, animations, and particle systems
static std::map<int, Sprite> sprites;
static std::map<int, SpriteAnimation> animations;
static std::map<int, ParticleSystem> particleSystems;
static std::map<int, bool> soundLoops;
static std::map<int, float> soundVolumes;
static std::map<int, float> soundPitches;
static int nextSpriteId = 1;
static int nextAnimationId = 1;
static int nextParticleSystemId = 1;
// Sprite structure
struct Sprite {
int textureId;
int x, y;
int width, height;
float speed;
Sprite() : textureId(0), x(0), y(0), width(0), height(0), speed(1.0f) {}
Sprite(int tid, int px, int py, int w, int h)
: textureId(tid), x(px), y(py), width(w), height(h), speed(1.0f) {}
};
// Sprite animation structure
struct SpriteAnimation {
int textureId;
int frameWidth, frameHeight;
int totalFrames;
int frameDelay;
int currentFrame;
int frameCounter;
bool playing;
bool paused;
SpriteAnimation() : textureId(0), frameWidth(0), frameHeight(0), totalFrames(0), frameDelay(0),
currentFrame(0), frameCounter(0), playing(false), paused(false) {}
SpriteAnimation(int tid, int fw, int fh, int tf, int fd)
: textureId(tid), frameWidth(fw), frameHeight(fh), totalFrames(tf), frameDelay(fd),
currentFrame(0), frameCounter(0), playing(false), paused(false) {}
};
// Particle structure
struct Particle {
float x, y;
float vx, vy;
int life;
Color color;
Particle(float px, float py, float vvx, float vvy, int l, Color c)
: x(px), y(py), vx(vvx), vy(vvy), life(l), color(c) {}
};
// Particle system structure
struct ParticleSystem {
std::vector<Particle> particles;
int x, y;
int maxParticles;
int speed;
int life;
Color color;
ParticleSystem() : x(0), y(0), maxParticles(0), speed(0), life(0), color({0, 0, 0, 255}) {}
ParticleSystem(int px, int py, int count, int s, int l, int r, int g, int b)
: x(px), y(py), maxParticles(count), speed(s), life(l), color({(unsigned char)r, (unsigned char)g, (unsigned char)b, 255}) {
// Initialize particles
for (int i = 0; i < count; i++) {
float angle = (float)i / count * 2.0f * PI;
float vx = cos(angle) * speed;
float vy = sin(angle) * speed;
particles.emplace_back(x, y, vx, vy, life, color);
}
}
};
// Sprite management functions
int CreateSprite(int textureId, int x, int y, int width, int height) {
int id = nextSpriteId++;
sprites[id] = Sprite(textureId, x, y, width, height);
return id;
}
void DrawSprite(int spriteId, int x, int y) {
auto it = sprites.find(spriteId);
if (it != sprites.end()) {
Sprite& sprite = it->second;
// For now, just draw a colored rectangle representing the sprite
DrawRectangle(x, y, sprite.width, sprite.height, RED);
}
}
void MoveSprite(int spriteId, int dx, int dy) {
auto it = sprites.find(spriteId);
if (it != sprites.end()) {
it->second.x += dx;
it->second.y += dy;
}
}
void SetSpritePosition(int spriteId, int x, int y) {
auto it = sprites.find(spriteId);
if (it != sprites.end()) {
it->second.x = x;
it->second.y = y;
}
}
int GetSpriteX(int spriteId) {
auto it = sprites.find(spriteId);
return (it != sprites.end()) ? it->second.x : 0;
}
int GetSpriteY(int spriteId) {
auto it = sprites.find(spriteId);
return (it != sprites.end()) ? it->second.y : 0;
}
void SetSpriteSpeed(int spriteId, float speed) {
auto it = sprites.find(spriteId);
if (it != sprites.end()) {
it->second.speed = speed;
}
}
float GetSpriteSpeed(int spriteId) {
auto it = sprites.find(spriteId);
return (it != sprites.end()) ? it->second.speed : 0.0f;
}
// Collision detection functions
bool CheckSpriteCollision(int sprite1Id, int sprite2Id) {
auto it1 = sprites.find(sprite1Id);
auto it2 = sprites.find(sprite2Id);
if (it1 != sprites.end() && it2 != sprites.end()) {
Sprite& s1 = it1->second;
Sprite& s2 = it2->second;
return (s1.x < s2.x + s2.width && s1.x + s1.width > s2.x &&
s1.y < s2.y + s2.height && s1.y + s1.height > s2.y);
}
return false;
}
bool CheckSpritePointCollision(int spriteId, int x, int y) {
auto it = sprites.find(spriteId);
if (it != sprites.end()) {
Sprite& sprite = it->second;
return (x >= sprite.x && x < sprite.x + sprite.width &&
y >= sprite.y && y < sprite.y + sprite.height);
}
return false;
}
bool CheckSpriteRectCollision(int spriteId, int x, int y, int width, int height) {
auto it = sprites.find(spriteId);
if (it != sprites.end()) {
Sprite& sprite = it->second;
return (sprite.x < x + width && sprite.x + sprite.width > x &&
sprite.y < y + height && sprite.y + sprite.height > y);
}
return false;
}
// Animation system functions
int CreateSpriteAnimation(int textureId, int frameWidth, int frameHeight, int totalFrames, int frameDelay) {
int id = nextAnimationId++;
animations[id] = SpriteAnimation(textureId, frameWidth, frameHeight, totalFrames, frameDelay);
return id;
}
void PlaySpriteAnimation(int animationId) {
auto it = animations.find(animationId);
if (it != animations.end()) {
it->second.playing = true;
it->second.paused = false;
}
}
void PauseSpriteAnimation(int animationId) {
auto it = animations.find(animationId);
if (it != animations.end()) {
it->second.paused = true;
}
}
void StopSpriteAnimation(int animationId) {
auto it = animations.find(animationId);
if (it != animations.end()) {
it->second.playing = false;
it->second.paused = false;
it->second.currentFrame = 0;
it->second.frameCounter = 0;
}
}
void SetSpriteAnimationFrame(int animationId, int frame) {
auto it = animations.find(animationId);
if (it != animations.end()) {
it->second.currentFrame = std::max(0, std::min(frame, it->second.totalFrames - 1));
}
}
int GetSpriteAnimationFrame(int animationId) {
auto it = animations.find(animationId);
return (it != animations.end()) ? it->second.currentFrame : 0;
}
// Particle system functions
int CreateParticleSystem(int x, int y, int count, int speed, int life, int r, int g, int b) {
int id = nextParticleSystemId++;
particleSystems[id] = ParticleSystem(x, y, count, speed, life, r, g, b);
return id;
}
void UpdateParticleSystem(int particleSystemId) {
auto it = particleSystems.find(particleSystemId);
if (it != particleSystems.end()) {
ParticleSystem& ps = it->second;
for (auto& particle : ps.particles) {
if (particle.life > 0) {
particle.x += particle.vx;
particle.y += particle.vy;
particle.life--;
}
}
}
}
void DrawParticleSystem(int particleSystemId) {
auto it = particleSystems.find(particleSystemId);
if (it != particleSystems.end()) {
ParticleSystem& ps = it->second;
for (const auto& particle : ps.particles) {
if (particle.life > 0) {
DrawPixel(particle.x, particle.y, particle.color);
}
}
}
}
void SetParticleSystemPosition(int particleSystemId, int x, int y) {
auto it = particleSystems.find(particleSystemId);
if (it != particleSystems.end()) {
it->second.x = x;
it->second.y = y;
}
}
// Enhanced sound system functions
void PlaySoundLoop(int soundId) {
soundLoops[soundId] = true;
// Note: Actual looping would need to be implemented in the main game loop
}
void StopSoundLoop(int soundId) {
soundLoops[soundId] = false;
}
void SetSoundVolume(int soundId, float volume) {
soundVolumes[soundId] = std::max(0.0f, std::min(1.0f, volume));
}
float GetSoundVolume(int soundId) {
auto it = soundVolumes.find(soundId);
return (it != soundVolumes.end()) ? it->second : 1.0f;
}
void SetSoundPitch(int soundId, float pitch) {
soundPitches[soundId] = std::max(0.5f, std::min(2.0f, pitch));
}
float GetSoundPitch(int soundId) {
auto it = soundPitches.find(soundId);
return (it != soundPitches.end()) ? it->second : 1.0f;
}
// Enhanced text system functions
void DrawTextEx(const char* text, int x, int y, int fontSize, int spacing, Color color) {
(void)spacing; // Suppress unused parameter warning
// For now, use basic text drawing
DrawText(text, x, y, fontSize, color);
}
int MeasureText(const char* text, int fontSize) {
// For now, return approximate width
return strlen(text) * fontSize / 2;
}
void DrawTextCentered(const char* text, int x, int y, int fontSize, int spacing, Color color) {
(void)spacing; // Suppress unused parameter warning
int width = MeasureText(text, fontSize);
DrawText(text, x - width / 2, y, fontSize, color);
}
// Utility functions
int RandomInt(int min, int max) {
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(min, max);
return dis(gen);
}
float RandomFloat(float min, float max) {
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_real_distribution<float> dis(min, max);
return dis(gen);
}
float Distance(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
return sqrt(dx * dx + dy * dy);
}
float Angle(int x1, int y1, int x2, int y2) {
return atan2(y2 - y1, x2 - x1) * 180.0f / PI;
}
float Lerp(float a, float b, float t) {
return a + (b - a) * t;
}
float Clamp(float value, float min, float max) {
return std::max(min, std::min(max, value));
}
} // namespace bas