mirror of
https://forge.univ-lyon1.fr/tplifap4/dungeonanddeamon.git
synced 2024-02-27 13:31:50 +01:00
290 lines
5.1 KiB
C++
290 lines
5.1 KiB
C++
#include "GestionPlayer.h"
|
|
#include <SpriteFrames.hpp>
|
|
#include <ConfigFile.hpp>
|
|
#include <Input.hpp>
|
|
#include <ResourceLoader.hpp>
|
|
#include <Texture.hpp>
|
|
#include <InputEventKey.hpp>
|
|
GestionPlayer::GestionPlayer()
|
|
{
|
|
|
|
sprite_player_ptr = AnimatedSprite::_new();
|
|
texture_player_ptr.instance();
|
|
texture_player_ptr->_new();
|
|
}
|
|
|
|
void GestionPlayer::_register_methods()
|
|
{
|
|
Godot::print("register Player...");
|
|
register_method("_process", &GestionPlayer::_process);
|
|
register_method("_init", &GestionPlayer::_init);
|
|
register_method("_ready", &GestionPlayer::_ready);
|
|
Godot::print("register Player OK!");
|
|
|
|
}
|
|
|
|
void GestionPlayer::_process(float dt) // dt = deltaTime
|
|
{
|
|
playerState.right = false;
|
|
playerState.left = false;
|
|
|
|
traitementInput();
|
|
|
|
PlayerLogic(dt);
|
|
p.velocity = move_and_slide(p.velocity);
|
|
}
|
|
|
|
void GestionPlayer::_init()
|
|
{
|
|
createPlayer();
|
|
}
|
|
|
|
void GestionPlayer::_ready()
|
|
{
|
|
setupPlayer();
|
|
}
|
|
|
|
void GestionPlayer::createPlayer()
|
|
{
|
|
add_child(sprite_player_ptr);
|
|
}
|
|
|
|
void GestionPlayer::setupPlayer()
|
|
{
|
|
//Chargement de la texture
|
|
//Godot::print("SetUp de Texture");
|
|
texture_player_ptr = ResourceLoader::get_singleton()->load("res://Character/Hero Knight/HeroKnight/Layer 1_sprite_01.png");
|
|
|
|
//setup du sprite
|
|
//Godot::print("SetUp de Sprite");
|
|
Ref<SpriteFrames> spriteFrames = ResourceLoader::get_singleton()->load("res://Character/Hero Knight/HeroKnight/HeroKnight.tres");
|
|
sprite_player_ptr->set_sprite_frames(spriteFrames);
|
|
|
|
PoolStringArray AnimNames = spriteFrames->get_animation_names();
|
|
PoolStringArray::Read r = AnimNames.read();
|
|
Godot::print("Animations Names");
|
|
for (int i = 0; i < AnimNames.size(); ++i)
|
|
{
|
|
Godot::print(r[i]);
|
|
}
|
|
|
|
}
|
|
|
|
void GestionPlayer::traitementInput()
|
|
{
|
|
// manage Inputs
|
|
Input* i = Input::get_singleton();
|
|
|
|
// move in X directions
|
|
p.velocity.x = 0.0f; // rest x, keyboard action will change this
|
|
if (i->is_action_pressed("ui_left"))
|
|
playerState.left = true;
|
|
if (i->is_action_pressed("ui_right"))
|
|
playerState.right = true;
|
|
if (i->is_action_pressed("dash"))
|
|
playerState.dash = true;
|
|
if (i->is_action_pressed("attack"))
|
|
playerState.attack = true;
|
|
if (i->is_action_pressed("attack2"))
|
|
playerState.bigattack = true;
|
|
if (i->is_action_pressed("block"))
|
|
playerState.block = true;
|
|
|
|
|
|
|
|
|
|
// move in Y directions
|
|
if (i->is_action_pressed("ui_select"))
|
|
playerState.jump = true;
|
|
|
|
|
|
}
|
|
|
|
void GestionPlayer::PlayerLogic(float dt)
|
|
{
|
|
if (playerState.jump)
|
|
{
|
|
// attack en l'air
|
|
PlayerLogic_Jump(dt);
|
|
}
|
|
else
|
|
{
|
|
// attack au sol
|
|
if (on_ground)
|
|
{
|
|
PlayerLogic_OnGround(dt);
|
|
}
|
|
else
|
|
{ // freefall
|
|
// attack en l'air
|
|
PlayerLogic_FreeFall(dt);
|
|
}
|
|
}
|
|
}
|
|
|
|
void GestionPlayer::PlayerLogic_Jump(float dt)
|
|
{
|
|
if (on_ground)
|
|
{
|
|
p.velocity.y = power_jump;
|
|
}
|
|
else
|
|
{
|
|
p.velocity.y += gravity * dt;
|
|
}
|
|
if (p.velocity.y > 0.0f)
|
|
{
|
|
playerState.jump = false;
|
|
playerState.freefall = true;
|
|
}
|
|
else
|
|
{
|
|
if (playerState.left)
|
|
{
|
|
p.velocity.x = -speed;
|
|
sprite_player_ptr->play("Jump Up");
|
|
sprite_player_ptr->set_flip_h(true);
|
|
}
|
|
else if (playerState.right)
|
|
{
|
|
p.velocity.x = +speed;
|
|
sprite_player_ptr->play("Jump Up");
|
|
sprite_player_ptr->set_flip_h(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
void GestionPlayer::PlayerLogic_OnGround(float dt)
|
|
{
|
|
p.velocity.y = 0.0f;
|
|
if (playerState.attack)
|
|
{
|
|
sprite_player_ptr->play("attack");
|
|
}
|
|
else if (playerState.bigattack)
|
|
{
|
|
|
|
sprite_player_ptr->play("attack2");
|
|
|
|
}
|
|
else if (playerState.dash)
|
|
{
|
|
|
|
sprite_player_ptr->play("dash");
|
|
p.velocity.x =+ 50;
|
|
|
|
}
|
|
else if (playerState.block)
|
|
{
|
|
|
|
sprite_player_ptr->play("shield");
|
|
|
|
|
|
}
|
|
else if (playerState.left)
|
|
{
|
|
p.velocity.x = -speed;
|
|
sprite_player_ptr->play("Walk");
|
|
sprite_player_ptr->set_flip_h(true);
|
|
}
|
|
else if (playerState.right)
|
|
{
|
|
p.velocity.x = +speed;
|
|
sprite_player_ptr->play("Walk");
|
|
sprite_player_ptr->set_flip_h(false);
|
|
}
|
|
else if (pv <= 0)
|
|
{
|
|
sprite_player_ptr->play("mort");
|
|
}
|
|
else
|
|
{
|
|
sprite_player_ptr->play("Idle");
|
|
}
|
|
}
|
|
|
|
void GestionPlayer::PlayerLogic_FreeFall(float dt)
|
|
{
|
|
p.velocity.x = +speed;
|
|
if (playerState.left)
|
|
{
|
|
p.velocity.x = -speed;
|
|
sprite_player_ptr->play("jump Down");
|
|
sprite_player_ptr->set_flip_h(true);
|
|
}
|
|
else if (playerState.right)
|
|
{
|
|
p.velocity.x = +speed;
|
|
sprite_player_ptr->play("jump Down");
|
|
sprite_player_ptr->set_flip_h(false);
|
|
}
|
|
p.velocity.y += gravity * dt;
|
|
}
|
|
|
|
void GestionPlayer::setPosition(int x, int y)
|
|
{
|
|
Transform2D t;
|
|
Vector2 pos;
|
|
Size2 s;
|
|
|
|
//setup de la size
|
|
s.x = 1.0f;
|
|
s.y = 1.0f;
|
|
pos.x = x;
|
|
pos.y = y;
|
|
t.set_origin(pos);
|
|
t.scale(s);
|
|
|
|
set_transform(t);
|
|
|
|
//init Velocity
|
|
p.velocity = Vector2(0.0f, 0.0f);
|
|
}
|
|
/*
|
|
void GestionPlayer::droit()
|
|
{
|
|
Godot::print("Deplacement a droite");
|
|
p.velocity.x = speed;
|
|
}
|
|
|
|
void GestionPlayer::gauche()
|
|
{
|
|
Godot::print("Deplacement a gauche");
|
|
p.velocity.x = -speed;
|
|
}
|
|
|
|
void GestionPlayer::bas()
|
|
{
|
|
}
|
|
|
|
void GestionPlayer::saut()
|
|
{
|
|
|
|
}
|
|
|
|
void GestionPlayer::idle()
|
|
{
|
|
p.velocity.x = 0.0;
|
|
}
|
|
|
|
void GestionPlayer::dash()
|
|
{
|
|
p.velocity.x =+1000;
|
|
sprite_player_ptr->play("dash");
|
|
}
|
|
|
|
void GestionPlayer::attack()
|
|
{
|
|
sprite_player_ptr->play("attack");
|
|
}
|
|
|
|
void GestionPlayer::block()
|
|
{
|
|
sprite_player_ptr->play("block");
|
|
}
|
|
|
|
void GestionPlayer::bigattack()
|
|
{
|
|
sprite_player_ptr->play("attack2");
|
|
}
|
|
*/ |