fix the comp errors later
This commit is contained in:
parent
bd094bfa2b
commit
438a2c9a92
3 changed files with 146 additions and 60 deletions
|
@ -1,59 +1 @@
|
||||||
extends RigidBody2D
|
|
||||||
|
|
||||||
@export_category("Basic Stuff")
|
|
||||||
@export var Speed: int = 250
|
|
||||||
@export var Health: int = 5
|
|
||||||
@export var Direction: Vector2 = Vector2(-1, 0)
|
|
||||||
@export var ScoreAmount: int = 200
|
|
||||||
|
|
||||||
@export_category("Shooting")
|
|
||||||
@export var IsShooting: bool = false
|
|
||||||
@export var Bullet: PackedScene
|
|
||||||
@export var FireSpeed: float = 1
|
|
||||||
@export_range(0, 360) var BulletDirection: float = 0
|
|
||||||
var FireTimer: float = 0
|
|
||||||
|
|
||||||
@export_category("Other")
|
|
||||||
@export var IsBoss: bool = false
|
|
||||||
|
|
||||||
var DespawnTimer: float = 0
|
|
||||||
|
|
||||||
func _ready():
|
|
||||||
get_node("Arm").rotation_degrees = -BulletDirection
|
|
||||||
if Direction.x == 1:
|
|
||||||
get_node("AnimatedSprite2D").flip_h = true
|
|
||||||
|
|
||||||
func _integrate_forces(_state: PhysicsDirectBodyState2D):
|
|
||||||
self.linear_velocity = Direction * Speed
|
|
||||||
|
|
||||||
func _process(delta: float):
|
|
||||||
# Despawning
|
|
||||||
DespawnTimer += delta
|
|
||||||
|
|
||||||
# Despawn to free up processing time
|
|
||||||
if DespawnTimer >= 30 and IsBoss == false:
|
|
||||||
self.queue_free()
|
|
||||||
|
|
||||||
# Shooting
|
|
||||||
if IsShooting == true:
|
|
||||||
FireTimer += delta
|
|
||||||
if FireTimer >= FireSpeed:
|
|
||||||
var bullet = Bullet.instantiate()
|
|
||||||
bullet.rotato = -BulletDirection + 180
|
|
||||||
bullet.transform = get_node("Arm/Marker2D").global_transform
|
|
||||||
get_tree().current_scene.add_child(bullet)
|
|
||||||
FireTimer = 0
|
|
||||||
|
|
||||||
# Death
|
|
||||||
if Health <= 0:
|
|
||||||
Global.TotalScore += ScoreAmount
|
|
||||||
if IsBoss == true:
|
|
||||||
Despawn()
|
|
||||||
else:
|
|
||||||
self.queue_free()
|
|
||||||
|
|
||||||
# FOR ANIMATION ONLY
|
|
||||||
func Despawn():
|
|
||||||
self.queue_free()
|
|
||||||
# Laziness. Gets the waves.
|
|
||||||
get_tree().current_scene.get_node("EnemyWaves").WaveWaits[get_tree().current_scene.get_node("EnemyWaves").CurrentWave] = 0
|
|
||||||
|
|
114
src/Enemy.cpp
114
src/Enemy.cpp
|
@ -15,6 +15,120 @@ namespace godot {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Enemy::_bind_methods() {
|
void Enemy::_bind_methods() {
|
||||||
|
// Regular Vars
|
||||||
|
ClassDB::bind_method(D_METHOD("GetHealth"), &Enemy::GetHealth);
|
||||||
|
ClassDB::bind_method(D_METHOD("SetHealth", "Health"), &Enemy::SetHealth);
|
||||||
|
ClassDB::add_property("Enemy", PropertyInfo(Variant::INT, "Health"), "SetHealth", "GetHealth");
|
||||||
|
ClassDB::bind_method(D_METHOD("GetSpeed"), &Enemy::GetSpeed);
|
||||||
|
ClassDB::bind_method(D_METHOD("SetSpeed", "Speed"), &Enemy::SetSpeed);
|
||||||
|
ClassDB::add_property("Enemy", PropertyInfo(Variant::INT, "Speed"), "SetUp", "GetUp");
|
||||||
|
ClassDB::bind_method(D_METHOD("GetDirection"), &Enemy::GetDirection);
|
||||||
|
ClassDB::bind_method(D_METHOD("SetDirection", "Direction"), &Enemy::SetDirection);
|
||||||
|
ClassDB::add_property("Enemy", PropertyInfo(Variant::VECTOR2, "Direction"), "SetDirection", "GetDirection");
|
||||||
|
ClassDB::bind_method(D_METHOD("GetScoreAmount"), &Enemy::GetScoreAmount);
|
||||||
|
ClassDB::bind_method(D_METHOD("SetScoreAmount", "ScoreAmount"), &Enemy::SetScoreAmount);
|
||||||
|
ClassDB::add_property("Enemy", PropertyInfo(Variant::INT, "ScoreAmount"), "SetScoreAmount", "GetScoreAmount");
|
||||||
|
|
||||||
|
// Shooting
|
||||||
|
ClassDB::add_property_group("Enemy", "Shooting", "Shot");
|
||||||
|
ClassDB::bind_method(D_METHOD("GetIsShooting"), &Enemy::GetIsShooting);
|
||||||
|
ClassDB::bind_method(D_METHOD("SetIsShooting", "IsShooting"), &Enemy::SetIsShooting);
|
||||||
|
ClassDB::add_property("Enemy", PropertyInfo(Variant::BOOL, "ShotIsShooting"), "SetIsShooting", "GetIsShooting");
|
||||||
|
ClassDB::bind_method(D_METHOD("GetFireRate"), &Enemy::GetFireRate);
|
||||||
|
ClassDB::bind_method(D_METHOD("SetFireRate", "FireRate"), &Enemy::SetFireRate);
|
||||||
|
ClassDB::add_property("Enemy", PropertyInfo(Variant::FLOAT, "ShotBulletLocation"), "SetFireRate", "GetFireRate");
|
||||||
|
ClassDB::bind_method(D_METHOD("GetBulletDirection"), &Enemy::GetBulletDirection);
|
||||||
|
ClassDB::bind_method(D_METHOD("SetBulletDirection", "BulletDirection"), &Enemy::SetBulletDirection);
|
||||||
|
ClassDB::add_property("Enemy", PropertyInfo(Variant::FLOAT, "ShotBulletDirection"), "SetBulletDirection", "GetBulletDirection");
|
||||||
|
|
||||||
|
// Signals
|
||||||
|
ClassDB::add_signal("Enemy", MethodInfo("AddScore"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemy::_integrate_forces(PhysicsDirectBodyState2D* state) {
|
||||||
|
|
||||||
|
if (Engine::get_singleton()->is_editor_hint()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
set_linear_velocity(Direction * Speed);
|
||||||
|
}
|
||||||
|
void Enemy::_physics_process(double delta) {
|
||||||
|
|
||||||
|
if (Engine::get_singleton()->is_editor_hint()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DespawnTimer += delta;
|
||||||
|
|
||||||
|
// Memfree despawn
|
||||||
|
if (DespawnTimer >= 30) {
|
||||||
|
queue_free();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shooting
|
||||||
|
if (IsShooting == true) {
|
||||||
|
FireRateTimer += delta;
|
||||||
|
if (FireRateTimer >= FireRate) {
|
||||||
|
Bullet* bullet = memnew(Bullet);
|
||||||
|
bullet->set_position(BulletLocation + get_position());
|
||||||
|
bullet->SetTexture(BulletTexture);
|
||||||
|
bullet->SetDirection(BulletDirection);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters and Setters
|
||||||
|
int Enemy::GetHealth() const {
|
||||||
|
return Health;
|
||||||
|
}
|
||||||
|
int Enemy::GetSpeed() const {
|
||||||
|
return Speed;
|
||||||
|
}
|
||||||
|
Vector2 Enemy::GetDirection() const {
|
||||||
|
return Direction;
|
||||||
|
}
|
||||||
|
int Enemy::GetScoreAmount() const {
|
||||||
|
return ScoreAmount;
|
||||||
|
}
|
||||||
|
bool Enemy::GetIsShooting() const {
|
||||||
|
return IsShooting;
|
||||||
|
}
|
||||||
|
double Enemy::GetFireRate() const {
|
||||||
|
return FireRate;
|
||||||
|
}
|
||||||
|
Vector2 Enemy::GetBulletDirection() const {
|
||||||
|
return BulletDirection;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemy::SetHealth(const int health) {
|
||||||
|
Health = health;
|
||||||
|
}
|
||||||
|
void Enemy::SetSpeed(const int speed) {
|
||||||
|
Speed = speed;
|
||||||
|
}
|
||||||
|
void Enemy::SetDirection(const Vector2 direction) {
|
||||||
|
Direction = direction;
|
||||||
|
}
|
||||||
|
void Enemy::SetScoreAmount(const int scoreAmount) {
|
||||||
|
ScoreAmount = scoreAmount;
|
||||||
|
}
|
||||||
|
void Enemy::SetIsShooting(const bool isShooting) {
|
||||||
|
IsShooting = isShooting;
|
||||||
|
}
|
||||||
|
void Enemy::SetFireRate(const double fireRate) {
|
||||||
|
FireRate = fireRate;
|
||||||
|
}
|
||||||
|
void Enemy::SetBulletDirection(const Vector2 bulletDirection) {
|
||||||
|
BulletDirection = bulletDirection;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,13 +2,24 @@
|
||||||
#define ENEMY_HPP
|
#define ENEMY_HPP
|
||||||
|
|
||||||
#include <godot_cpp/classes/rigid_body2d.hpp>
|
#include <godot_cpp/classes/rigid_body2d.hpp>
|
||||||
|
#include <godot_cpp/classes/physics_direct_body_state2d.hpp>
|
||||||
|
|
||||||
|
#include "Bullet.hpp"
|
||||||
|
|
||||||
namespace godot {
|
namespace godot {
|
||||||
class Enemy : public RigidBody2D {
|
class Enemy : public RigidBody2D {
|
||||||
GDCLASS(Enemy, RigidBody2D)
|
GDCLASS(Enemy, RigidBody2D)
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int nothing = 0;
|
int Health = 5;
|
||||||
|
int Speed = 250;
|
||||||
|
Vector2 Direction = Vector2(-1, 0);
|
||||||
|
int ScoreAmount = 200;
|
||||||
|
|
||||||
|
bool IsShooting = false;
|
||||||
|
double FireRate = 1;
|
||||||
|
double FireRateTimer = 0;
|
||||||
|
Vector2 BulletDirection = Vector2(-1, 0);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
@ -17,6 +28,25 @@ namespace godot {
|
||||||
Enemy();
|
Enemy();
|
||||||
~Enemy();
|
~Enemy();
|
||||||
|
|
||||||
|
void _integrate_forces(PhysicsDirectBodyState2D* state) override;
|
||||||
|
void _physics_process(double delta) override;
|
||||||
|
|
||||||
|
// Getters and Setters
|
||||||
|
int GetHealth() const;
|
||||||
|
int GetSpeed() const;
|
||||||
|
Vector2 GetDirection() const;
|
||||||
|
int GetScoreAmount() const;
|
||||||
|
bool GetIsShooting() const;
|
||||||
|
double GetFireRate() const;
|
||||||
|
double GetBulletDirection() const;
|
||||||
|
|
||||||
|
void SetHealth(const int health);
|
||||||
|
void SetSpeed(const int speed);
|
||||||
|
void SetDirection(const Vector2 direction);
|
||||||
|
void SetScoreAmount(const int scoreAmount);
|
||||||
|
void SetIsShooting(const bool isShooting);
|
||||||
|
void SetFireRate(const double fireRate);
|
||||||
|
void SetBulletDirection(const double bulletDirection);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue