-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectile.cpp
More file actions
84 lines (69 loc) · 2.5 KB
/
projectile.cpp
File metadata and controls
84 lines (69 loc) · 2.5 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
#include <SDL3/SDL.h>
#include <math.h>
#include "projectile.h"
#include "physics_world.h"
#include "defines.h"
Projectile::Projectile()
: Object(0, 0, 6, 6), angle(0), lifetime(30.0f), bounceCount(0),
maxBounces(5), expired(false), coordx(0), coordy(0), speed(0),
color(map_rgb(255, 255, 0)) {
}
Projectile::Projectile(float x, float y, float angle, float speed)
: Object(x, y, 6, 6), angle(angle), lifetime(30.0f), bounceCount(0),
maxBounces(5), expired(false), coordx(0), coordy(0), speed(speed),
color(map_rgb(255, 255, 0)) {
vel.x = cos(angle) * speed;
vel.y = sin(angle) * speed;
}
void Projectile::initPhysics(PhysicsWorld& world) {
physicsWorldPtr = &world;
// Create as dynamic body with high restitution for bouncing
physicsBody = world.createBody(this, PhysicsBodyType::DYNAMIC, 0.1f, 0.0f, 1.0f);
if (b2Body_IsValid(physicsBody)) {
b2Body_SetLinearDamping(physicsBody, 0.0f);
b2Body_SetGravityScale(physicsBody, 0.0f); // No gravity for projectiles
b2Body_SetBullet(physicsBody, true); // Continuous collision detection
// Set initial velocity
world.setLinearVelocity(physicsBody, vel.x, vel.y);
}
}
void Projectile::syncFromPhysics() {
if (!physicsWorldPtr || !b2Body_IsValid(physicsBody)) return;
b2Vec2 physPos = physicsWorldPtr->getPosition(physicsBody);
b2Vec2 physVel = physicsWorldPtr->getLinearVelocity(physicsBody);
pos.x = physPos.x;
pos.y = physPos.y;
vel.x = physVel.x;
vel.y = physVel.y;
// Update angle based on velocity direction
if (vel.x != 0 || vel.y != 0) {
angle = atan2(vel.y, vel.x);
}
}
void Projectile::update(void) {
if (physicsWorldPtr && b2Body_IsValid(physicsBody)) {
syncFromPhysics();
} else {
pos.x += vel.x;
pos.y += vel.y;
}
// Decrease lifetime
lifetime -= 1.0f / 60.0f; // Assuming 60 FPS
// Check expiration conditions
if (lifetime <= 0 || bounceCount >= maxBounces) {
expired = true;
}
}
void Projectile::draw(void) {
if (expired) return;
computeRect();
// Draw projectile as a small bright dot/circle
float radius = width2;
draw_filled_ellipse(pos.x, pos.y, radius, radius, color);
// Draw a trail/tail
float trailLen = 10.0f;
float trailX = pos.x - cos(angle) * trailLen;
float trailY = pos.y - sin(angle) * trailLen;
GameColor trailColor = map_rgb(255, 200, 0);
draw_line(pos.x, pos.y, trailX, trailY, trailColor, 2.0f);
}