more stuff for the player

This commit is contained in:
CatAClock 2025-06-12 16:59:08 -07:00
parent dcd23598da
commit 3a1a38ff60
5 changed files with 69 additions and 31 deletions

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=12 format=3 uid="uid://bjbccem28ir8r"] [gd_scene load_steps=13 format=3 uid="uid://bjbccem28ir8r"]
[ext_resource type="Texture2D" uid="uid://bcbjh2amre7ke" path="res://non-godot/Textures/Plane/PlaneUp.png" id="1_2h3i0"] [ext_resource type="Texture2D" uid="uid://bcbjh2amre7ke" path="res://non-godot/Textures/Plane/PlaneUp.png" id="1_2h3i0"]
[ext_resource type="Texture2D" uid="uid://c5kt81t5h4vgn" path="res://non-godot/Textures/Plane/Plane.png" id="3_5g2vf"] [ext_resource type="Texture2D" uid="uid://c5kt81t5h4vgn" path="res://non-godot/Textures/Plane/Plane.png" id="3_5g2vf"]
@ -6,6 +6,8 @@
[ext_resource type="Texture2D" uid="uid://bff4pkabs2ecf" path="res://non-godot/Textures/Plane/PlaneDown.png" id="3_edjcf"] [ext_resource type="Texture2D" uid="uid://bff4pkabs2ecf" path="res://non-godot/Textures/Plane/PlaneDown.png" id="3_edjcf"]
[ext_resource type="AudioStream" uid="uid://do4a5aj5a0216" path="res://non-godot/Sound/Shoot.ogg" id="4_m3gyy"] [ext_resource type="AudioStream" uid="uid://do4a5aj5a0216" path="res://non-godot/Sound/Shoot.ogg" id="4_m3gyy"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_s07ij"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_u1ywu"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_u1ywu"]
size = Vector2(1920, 720) size = Vector2(1920, 720)
@ -15,8 +17,8 @@ emission_shape = 3
emission_box_extents = Vector3(0, 120, 1) emission_box_extents = Vector3(0, 120, 1)
direction = Vector3(-1, 0, 0) direction = Vector3(-1, 0, 0)
spread = 0.0 spread = 0.0
initial_velocity_min = 1950.0 initial_velocity_min = 3000.0
initial_velocity_max = 1950.0 initial_velocity_max = 3000.0
gravity = Vector3(0, 0, 0) gravity = Vector3(0, 0, 0)
scale_max = 8.0 scale_max = 8.0
color = Color(0.513233, 0.513233, 0.513233, 1) color = Color(0.513233, 0.513233, 0.513233, 1)
@ -160,11 +162,14 @@ Left = &"Left"
Up = &"Up" Up = &"Up"
Right = &"Right" Right = &"Right"
Down = &"Down" Down = &"Down"
scale = Vector2(0.25, 0.25) Shoot = &"Shoot"
collision_layer = 4 SpeedUp = &"SpeedUp"
collision_mask = 4 scale = Vector2(0.15, 0.15)
motion_mode = 1 motion_mode = 1
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_s07ij")
[node name="Area2D" type="Area2D" parent="."] [node name="Area2D" type="Area2D" parent="."]
position = Vector2(3, 3) position = Vector2(3, 3)

View file

@ -3,10 +3,6 @@ extends Player
var Invulnerable: bool = false var Invulnerable: bool = false
var InvulnerabilityTimer: float = 0 var InvulnerabilityTimer: float = 0
@export_category("Shooting")
@export var Bullet: PackedScene
var FireRateTimer: float
@export_category("Other") @export_category("Other")
@export var OnDeathScene: PackedScene @export var OnDeathScene: PackedScene
@ -15,8 +11,6 @@ var PlayerControl: bool = true
signal PlayerTookDamage(Health: int) signal PlayerTookDamage(Health: int)
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
FireRateTimer += delta
# Invulnerability. This is the segment that runs after taking damage. # Invulnerability. This is the segment that runs after taking damage.
if Invulnerable == true: if Invulnerable == true:
InvulnerabilityTimer += delta InvulnerabilityTimer += delta
@ -24,20 +18,6 @@ func _physics_process(delta: float) -> void:
Invulnerable = false Invulnerable = false
InvulnerabilityTimer = 0 InvulnerabilityTimer = 0
if PlayerControl:
# Shooting
if Input.get_action_raw_strength("Shoot") && FireRateTimer >= FireRate:
var bullet = Bullet.instantiate()
bullet.transform = get_node("Gun/Marker2D").global_transform
get_tree().current_scene.add_child(bullet)
FireRateTimer = 0
get_node("Gun/AudioStreamPlayer").play()
# If the player losses control (because they are dead)
else:
@warning_ignore("integer_division")
self.velocity.y = Speed / 2
self.velocity.x = move_toward(velocity.x, 0, Speed)
# Don't use move_and_collide() because it sticks to the walls. # Don't use move_and_collide() because it sticks to the walls.
move_and_slide() move_and_slide()

View file

@ -9,6 +9,8 @@
#include <godot_cpp/classes/collision_shape2d.hpp> #include <godot_cpp/classes/collision_shape2d.hpp>
#include <godot_cpp/classes/rectangle_shape2d.hpp> #include <godot_cpp/classes/rectangle_shape2d.hpp>
#include <godot_cpp/core/print_string.hpp>
namespace godot { namespace godot {
Player::Player() { Player::Player() {
CollisionShape2D* Collision = memnew(CollisionShape2D); CollisionShape2D* Collision = memnew(CollisionShape2D);
@ -41,6 +43,9 @@ namespace godot {
ClassDB::bind_method(D_METHOD("GetFireRate"), &Player::GetFireRate); ClassDB::bind_method(D_METHOD("GetFireRate"), &Player::GetFireRate);
ClassDB::bind_method(D_METHOD("SetFireRate", "FireRate"), &Player::SetFireRate); ClassDB::bind_method(D_METHOD("SetFireRate", "FireRate"), &Player::SetFireRate);
ClassDB::add_property("Player", PropertyInfo(Variant::FLOAT, "FireRate"), "SetFireRate", "GetFireRate"); ClassDB::add_property("Player", PropertyInfo(Variant::FLOAT, "FireRate"), "SetFireRate", "GetFireRate");
ClassDB::bind_method(D_METHOD("GetBullet"), &Player::GetBullet);
ClassDB::bind_method(D_METHOD("SetBullet", "Bullet"), &Player::SetBullet);
//ClassDB::add_property("Player", PropertyInfo(PackedScene, "Bullet"), "SetBullet", "GetBullet");
// Textures // Textures
ClassDB::bind_method(D_METHOD("GetPlayerUp"), &Player::GetPlayerUp); ClassDB::bind_method(D_METHOD("GetPlayerUp"), &Player::GetPlayerUp);
@ -66,24 +71,43 @@ namespace godot {
ClassDB::bind_method(D_METHOD("GetDown"), &Player::GetDown); ClassDB::bind_method(D_METHOD("GetDown"), &Player::GetDown);
ClassDB::bind_method(D_METHOD("SetDown", "Down"), &Player::SetDown); ClassDB::bind_method(D_METHOD("SetDown", "Down"), &Player::SetDown);
ClassDB::add_property("Player", PropertyInfo(Variant::STRING_NAME, "Down"), "SetDown", "GetDown"); ClassDB::add_property("Player", PropertyInfo(Variant::STRING_NAME, "Down"), "SetDown", "GetDown");
ClassDB::bind_method(D_METHOD("GetShoot"), &Player::GetShoot);
ClassDB::bind_method(D_METHOD("SetShoot", "Shoot"), &Player::SetShoot);
ClassDB::add_property("Player", PropertyInfo(Variant::STRING_NAME, "Shoot"), "SetShoot", "GetShoot");
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, "SpeedUp"), "SetSpeedUp", "GetSpeedUp");
} }
void Player::_physics_process(double delta) { void Player::_physics_process(double delta) {
int TheSpeed = Speed;
FireRateTimer += delta;
Sprite->set_texture(PlayerNeutral); Sprite->set_texture(PlayerNeutral);
if (Engine::get_singleton()->is_editor_hint()) { if (Engine::get_singleton()->is_editor_hint()) {
return; return;
} }
int XDirection = Input::get_singleton()->get_axis(Left, Right); int XDirection = Input::get_singleton()->get_axis(Left, Right);
int YDirection = Input::get_singleton()->get_axis(Up, Down); int YDirection = Input::get_singleton()->get_axis(Up, Down);
set_velocity(Vector2(XDirection, YDirection).normalized() * Speed); if (Input::get_singleton()->get_action_raw_strength(SpeedUp) != 0) {
// change thing when going up and down. TheSpeed *= 2;
}
set_velocity(Vector2(XDirection, YDirection).normalized() * TheSpeed);
// Change thing when going up and down.
if (YDirection == 1) { if (YDirection == 1) {
Sprite->set_texture(PlayerDown); Sprite->set_texture(PlayerDown);
} else if (YDirection == -1) { } else if (YDirection == -1) {
Sprite->set_texture(PlayerUp); Sprite->set_texture(PlayerUp);
} }
// Shooting.
if (Input::get_singleton()->get_action_raw_strength(Shoot) != 0) {
print_line("Fire!");
FireRateTimer = 0;
}
move_and_slide(); move_and_slide();
} }
@ -100,6 +124,9 @@ namespace godot {
float Player::GetFireRate() const { float Player::GetFireRate() const {
return FireRate; return FireRate;
} }
PackedScene* Player::GetBullet() {
return Bullet;
}
Ref<CompressedTexture2D> Player::GetPlayerUp() const { Ref<CompressedTexture2D> Player::GetPlayerUp() const {
return PlayerUp; return PlayerUp;
} }
@ -121,7 +148,13 @@ namespace godot {
StringName Player::GetDown() const { StringName Player::GetDown() const {
return Down; return Down;
} }
StringName Player::GetShoot() const {
return Shoot;
}
StringName Player::GetSpeedUp() const {
return SpeedUp;
}
void Player::SetHealth(const int health) { void Player::SetHealth(const int health) {
Health = health; Health = health;
} }
@ -134,6 +167,9 @@ namespace godot {
void Player::SetFireRate(const float fireRate) { void Player::SetFireRate(const float fireRate) {
FireRate = fireRate; FireRate = fireRate;
} }
void Player::SetBullet(PackedScene* bullet) {
Bullet = bullet;
}
void Player::SetPlayerUp(const Ref<CompressedTexture2D>& playerUp) { void Player::SetPlayerUp(const Ref<CompressedTexture2D>& playerUp) {
PlayerUp = playerUp; PlayerUp = playerUp;
} }
@ -155,4 +191,10 @@ namespace godot {
void Player::SetDown(const StringName down) { void Player::SetDown(const StringName down) {
Down = down; Down = down;
} }
void Player::SetShoot(const StringName shoot) {
Shoot = shoot;
}
void Player::SetSpeedUp(const StringName speedUp) {
SpeedUp = speedUp;
}
}; };

View file

@ -6,6 +6,7 @@
#include <godot_cpp/classes/character_body2d.hpp> #include <godot_cpp/classes/character_body2d.hpp>
#include <godot_cpp/classes/compressed_texture2d.hpp> #include <godot_cpp/classes/compressed_texture2d.hpp>
#include <godot_cpp/classes/sprite2d.hpp> #include <godot_cpp/classes/sprite2d.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
namespace godot { namespace godot {
class Player : public CharacterBody2D { class Player : public CharacterBody2D {
@ -16,10 +17,12 @@ namespace godot {
int Health = 3; int Health = 3;
int Speed = 50; int Speed = 50;
bool Invulnerable = false; bool Invulnerable = false;
float InvulnerabilityTime = 3.0; double InvulnerabilityTime = 3.0;
// Killing // Killing
float FireRate = 0.04; PackedScene* Bullet;
double FireRate = 0.04;
double FireRateTimer = 0;
//Textures //Textures
Ref<CompressedTexture2D> PlayerUp; Ref<CompressedTexture2D> PlayerUp;
@ -31,6 +34,8 @@ namespace godot {
StringName Up; StringName Up;
StringName Right; StringName Right;
StringName Down; StringName Down;
StringName Shoot;
StringName SpeedUp;
// Nodes // Nodes
Sprite2D* Sprite; Sprite2D* Sprite;
@ -49,6 +54,7 @@ namespace godot {
int GetSpeed() const; int GetSpeed() const;
float GetInvulnerabilityTime() const; float GetInvulnerabilityTime() const;
float GetFireRate() const; float GetFireRate() const;
PackedScene* GetBullet();
Ref<CompressedTexture2D> GetPlayerUp() const; Ref<CompressedTexture2D> GetPlayerUp() const;
Ref<CompressedTexture2D> GetPlayerNeutral() const; Ref<CompressedTexture2D> GetPlayerNeutral() const;
Ref<CompressedTexture2D> GetPlayerDown() const; Ref<CompressedTexture2D> GetPlayerDown() const;
@ -56,11 +62,14 @@ namespace godot {
StringName GetUp() const; StringName GetUp() const;
StringName GetRight() const; StringName GetRight() const;
StringName GetDown() const; StringName GetDown() const;
StringName GetShoot() const;
StringName GetSpeedUp() const;
void SetHealth(const int health); void SetHealth(const int health);
void SetSpeed(const int speed); void SetSpeed(const int speed);
void SetInvulnerabilityTime(const float invulnerabilityTime); void SetInvulnerabilityTime(const float invulnerabilityTime);
void SetFireRate(const float fireRate); void SetFireRate(const float fireRate);
void SetBullet(PackedScene* bullet);
void SetPlayerUp(const Ref<CompressedTexture2D>& playerUp); void SetPlayerUp(const Ref<CompressedTexture2D>& playerUp);
void SetPlayerNeutral(const Ref<CompressedTexture2D>& playerNeutral); void SetPlayerNeutral(const Ref<CompressedTexture2D>& playerNeutral);
void SetPlayerDown(const Ref<CompressedTexture2D>& playerDown); void SetPlayerDown(const Ref<CompressedTexture2D>& playerDown);
@ -68,6 +77,8 @@ namespace godot {
void SetUp(const StringName up); void SetUp(const StringName up);
void SetRight(const StringName right); void SetRight(const StringName right);
void SetDown(const StringName down); void SetDown(const StringName down);
void SetShoot(const StringName shoot);
void SetSpeedUp(const StringName speedUp);
}; };
}; };