Collisions now work. It's safe to pushie

This commit is contained in:
CatAClock 2025-07-22 17:14:26 -07:00
parent e06921306a
commit fd13dfb423
5 changed files with 52 additions and 8 deletions

View file

@ -17,6 +17,8 @@ namespace godot {
add_child(Sprite);
add_child(Collision);
add_to_group("Bullet");
}
Bullet::~Bullet() {

View file

@ -1,4 +1,5 @@
#include "Enemy.hpp"
#include "Player.hpp"
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/property_info.hpp>
@ -17,6 +18,9 @@ namespace godot {
add_child(Collision);
add_child(Sprite);
add_to_group("Enemy");
set_contact_monitor(true);
}
Enemy::~Enemy() {
@ -81,6 +85,7 @@ namespace godot {
Sprite->set_flip_h(false);
}
// Anything before this is done within Godot Engine. Anything after is doing Watersports.
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
@ -99,16 +104,25 @@ namespace godot {
bullet->set_position(BulletLocation + get_position());
bullet->SetTexture(BulletTexture);
bullet->SetDirection(BulletDirection);
bullet->add_to_group("Enemy");
bullet->add_collision_exception_with(get_node<Enemy>(get_path()));
get_parent()->add_child(bullet);
FireRateTimer = 0;
}
}
// Dying.
if (Health <= 0) {
emit_signal("AddScore");
queue_free();
for (int i = 0; i < get_colliding_bodies().size(); i++) {
auto Collide = Object::cast_to<Node2D>(get_colliding_bodies()[i]);
if (Collide->is_in_group("Player") && Object::cast_to<Player>(get_colliding_bodies()[i])->GetInvulnerability() == false) {
Health -= 1;
} else if (Collide->is_in_group("Enemy")) {
add_collision_exception_with(get_node<Node>(Collide->get_path()));
}
// Dying
if (Health == 0) {
emit_signal("AddScore");
queue_free();
}
}
}

View file

@ -7,8 +7,6 @@
#include <godot_cpp/classes/kinematic_collision2d.hpp>
#include <godot_cpp/core/print_string.hpp>
namespace godot {
Player::Player() {
Collision = memnew(CollisionShape2D);
@ -19,6 +17,8 @@ namespace godot {
add_child(Collision);
add_child(Sprite);
add_to_group("Player");
}
Player::~Player() {
@ -80,6 +80,9 @@ namespace godot {
ClassDB::bind_method(D_METHOD("GetSpeedUp"), &Player::GetSpeedUp);
ClassDB::bind_method(D_METHOD("SetSpeedUp", "SpeedUp"), &Player::SetSpeedUp);
ClassDB::add_property("Player", PropertyInfo(Variant::STRING_NAME, "InputSpeedUp"), "SetSpeedUp", "GetSpeedUp");
// Individuals
ClassDB::bind_method(D_METHOD("GetInvulnerability"), &Player::GetInvulnerability);
}
void Player::_physics_process(double delta) {
@ -88,25 +91,30 @@ namespace godot {
Sprite->set_texture(PlayerNeutral);
CollisionShape->set_size(Vector2(PlayerNeutral->get_width(), PlayerNeutral->get_height()));
// Anything before this is done in engine. Anything after is shot on sight.
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
// invulnerable
if (Invulnerable == true) {
set_modulate(Color(1, 1, 1, 0.2));
InvulnerabilityTimer += delta;
if (InvulnerabilityTimer >= InvulnerabilityTime) {
InvulnerabilityTimer = 0;
Invulnerable = false;
set_modulate(Color(1, 1, 1, 1));
}
}
// Setting speeds
int XDirection = Input::get_singleton()->get_axis(Left, Right);
int YDirection = Input::get_singleton()->get_axis(Up, Down);
if (Input::get_singleton()->get_action_raw_strength(SpeedUp) != 0) {
TheSpeed *= 2;
}
// Player control.
if (PlayerControl == true) {
set_velocity(Vector2(XDirection, YDirection).normalized() * TheSpeed);
// Change thing when going up and down.
@ -125,20 +133,37 @@ namespace godot {
bullet->set_position(BulletLocation + get_position());
bullet->SetTexture(BulletTexture);
bullet->SetDirection(Vector2(1, 0));
bullet->add_to_group("Player");
bullet->add_collision_exception_with(get_node<Player>(get_path()));
get_parent()->add_child(bullet);
FireRateTimer = 0;
}
move_and_slide();
// Collisions
for (int i = 0; i < get_slide_collision_count(); i++) {
Ref<KinematicCollision2D> Collide = get_slide_collision(i);
// TODO: replace this later
print_line(Collide);
if (Object::cast_to<Node>(Collide->get_collider())->is_in_group("Enemy") && Invulnerable == false) {
Invulnerable = true;
Health -= 1;
Object::cast_to<Node>(Collide->get_collider())->queue_free();
} else if (Object::cast_to<Node>(Collide->get_collider())->is_in_group("Bullet")) {
Object::cast_to<Node>(Collide->get_collider())->queue_free();
}
// Death
if (Health == 0) {
PlayerControl = false;
}
}
}
// Individual Getters
bool Player::GetInvulnerability() const {
return Invulnerable;
}
// Getters and Setters
int Player::GetHealth() const {
return Health;

View file

@ -57,6 +57,9 @@ namespace godot {
void _physics_process(double delta) override;
// Indivudual Methods
bool GetInvulnerability() const;
// Getters and Setters
int GetHealth() const;
int GetSpeed() const;