158 lines
5.7 KiB
C++
158 lines
5.7 KiB
C++
#include "Player.hpp"
|
|
|
|
#include <godot_cpp/core/class_db.hpp>
|
|
#include <godot_cpp/core/property_info.hpp>
|
|
#include <godot_cpp/core/memory.hpp>
|
|
#include <godot_cpp/classes/engine.hpp>
|
|
|
|
#include <godot_cpp/classes/area2d.hpp>
|
|
#include <godot_cpp/classes/collision_shape2d.hpp>
|
|
#include <godot_cpp/classes/rectangle_shape2d.hpp>
|
|
|
|
namespace godot {
|
|
Player::Player() {
|
|
CollisionShape2D* Collision = memnew(CollisionShape2D);
|
|
RectangleShape2D* CollisionShape = memnew(RectangleShape2D);
|
|
Sprite = memnew(Sprite2D);
|
|
|
|
Collision->set_shape(CollisionShape);
|
|
|
|
add_child(Collision);
|
|
add_child(Sprite);
|
|
}
|
|
|
|
Player::~Player() {
|
|
queue_free();
|
|
}
|
|
|
|
void Player::_bind_methods() {
|
|
// Regular Variables
|
|
ClassDB::bind_method(D_METHOD("GetHealth"), &Player::GetHealth);
|
|
ClassDB::bind_method(D_METHOD("SetHealth", "Health"), &Player::SetHealth);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::INT, "Health"), "SetHealth", "GetHealth");
|
|
ClassDB::bind_method(D_METHOD("GetSpeed"), &Player::GetSpeed);
|
|
ClassDB::bind_method(D_METHOD("SetSpeed", "Speed"), &Player::SetSpeed);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::INT, "Speed"), "SetSpeed", "GetSpeed");
|
|
ClassDB::bind_method(D_METHOD("GetInvulnerabilityTime"), &Player::GetInvulnerabilityTime);
|
|
ClassDB::bind_method(D_METHOD("SetInvulnerabilityTime", "InvulnerabilityTime"), &Player::SetInvulnerabilityTime);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::FLOAT, "InvulnerabilityTime"), "SetInvulnerabilityTime", "GetInvulnerabilityTime");
|
|
|
|
// Killing
|
|
ClassDB::bind_method(D_METHOD("GetFireRate"), &Player::GetFireRate);
|
|
ClassDB::bind_method(D_METHOD("SetFireRate", "FireRate"), &Player::SetFireRate);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::FLOAT, "FireRate"), "SetFireRate", "GetFireRate");
|
|
|
|
// Textures
|
|
ClassDB::bind_method(D_METHOD("GetPlayerUp"), &Player::GetPlayerUp);
|
|
ClassDB::bind_method(D_METHOD("SetPlayerUp", "PlayerUp"), &Player::SetPlayerUp);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::OBJECT, "PlayerUp"), "SetPlayerUp", "GetPlayerUp");
|
|
ClassDB::bind_method(D_METHOD("GetPlayerNeutral"), &Player::GetPlayerNeutral);
|
|
ClassDB::bind_method(D_METHOD("SetPlayerNeutral", "PlayerNeutral"), &Player::SetPlayerNeutral);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::OBJECT, "PlayerNeutral"), "SetPlayerNeutral", "GetPlayerNeutral");
|
|
ClassDB::bind_method(D_METHOD("GetPlayerDown"), &Player::GetPlayerDown);
|
|
ClassDB::bind_method(D_METHOD("SetPlayerDown", "PlayerDown"), &Player::SetPlayerDown);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::OBJECT, "PlayerDown"), "SetPlayerDown", "GetPlayerDown");
|
|
|
|
// Keys
|
|
ClassDB::bind_method(D_METHOD("GetLeft"), &Player::GetLeft);
|
|
ClassDB::bind_method(D_METHOD("SetLeft", "Left"), &Player::SetLeft);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::STRING_NAME, "Left"), "SetLeft", "GetLeft");
|
|
ClassDB::bind_method(D_METHOD("GetUp"), &Player::GetUp);
|
|
ClassDB::bind_method(D_METHOD("SetUp", "Up"), &Player::SetUp);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::STRING_NAME, "Up"), "SetUp", "GetUp");
|
|
ClassDB::bind_method(D_METHOD("GetRight"), &Player::GetRight);
|
|
ClassDB::bind_method(D_METHOD("SetRight", "Right"), &Player::SetRight);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::STRING_NAME, "Right"), "SetRight", "GetRight");
|
|
ClassDB::bind_method(D_METHOD("GetDown"), &Player::GetDown);
|
|
ClassDB::bind_method(D_METHOD("SetDown", "Down"), &Player::SetDown);
|
|
ClassDB::add_property("Player", PropertyInfo(Variant::STRING_NAME, "Down"), "SetDown", "GetDown");
|
|
}
|
|
|
|
void Player::_physics_process(double delta) {
|
|
Sprite->set_texture(PlayerNeutral);
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
return;
|
|
}
|
|
int XDirection = Input::get_singleton()->get_axis(Left, Right);
|
|
int YDirection = Input::get_singleton()->get_axis(Up, Down);
|
|
|
|
set_velocity(Vector2(XDirection, YDirection).normalized() * Speed);
|
|
// change thing when going up and down.
|
|
if (YDirection == 1) {
|
|
Sprite->set_texture(PlayerDown);
|
|
} else if (YDirection == -1) {
|
|
Sprite->set_texture(PlayerUp);
|
|
}
|
|
|
|
move_and_slide();
|
|
}
|
|
|
|
// Getters and Setters
|
|
int Player::GetHealth() const {
|
|
return Health;
|
|
}
|
|
int Player::GetSpeed() const {
|
|
return Speed;
|
|
}
|
|
float Player::GetInvulnerabilityTime() const {
|
|
return InvulnerabilityTime;
|
|
}
|
|
float Player::GetFireRate() const {
|
|
return FireRate;
|
|
}
|
|
Ref<CompressedTexture2D> Player::GetPlayerUp() const {
|
|
return PlayerUp;
|
|
}
|
|
Ref<CompressedTexture2D> Player::GetPlayerNeutral() const {
|
|
return PlayerNeutral;
|
|
}
|
|
Ref<CompressedTexture2D> Player::GetPlayerDown() const {
|
|
return PlayerDown;
|
|
}
|
|
StringName Player::GetLeft() const {
|
|
return Left;
|
|
}
|
|
StringName Player::GetUp() const {
|
|
return Up;
|
|
}
|
|
StringName Player::GetRight() const {
|
|
return Right;
|
|
}
|
|
StringName Player::GetDown() const {
|
|
return Down;
|
|
}
|
|
|
|
void Player::SetHealth(const int health) {
|
|
Health = health;
|
|
}
|
|
void Player::SetSpeed(const int speed) {
|
|
Speed = speed;
|
|
}
|
|
void Player::SetInvulnerabilityTime(const float invulnerabilityTime) {
|
|
InvulnerabilityTime = invulnerabilityTime;
|
|
}
|
|
void Player::SetFireRate(const float fireRate) {
|
|
FireRate = fireRate;
|
|
}
|
|
void Player::SetPlayerUp(const Ref<CompressedTexture2D>& playerUp) {
|
|
PlayerUp = playerUp;
|
|
}
|
|
void Player::SetPlayerNeutral(const Ref<CompressedTexture2D>& playerNeutral) {
|
|
PlayerNeutral = playerNeutral;
|
|
}
|
|
void Player::SetPlayerDown(const Ref<CompressedTexture2D>& playerDown) {
|
|
PlayerDown = playerDown;
|
|
}
|
|
void Player::SetLeft(const StringName left) {
|
|
Left = left;
|
|
}
|
|
void Player::SetUp(const StringName up) {
|
|
Up = up;
|
|
}
|
|
void Player::SetRight(const StringName right) {
|
|
Right = right;
|
|
}
|
|
void Player::SetDown(const StringName down) {
|
|
Down = down;
|
|
}
|
|
};
|