Skip to content

Commit

Permalink
control
Browse files Browse the repository at this point in the history
  • Loading branch information
windpeeskillet committed Nov 27, 2019
1 parent f5f34e0 commit 0e4e383
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Scripts/Player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "Player.h"
#include "../Mx/Input/MxInput.h"
#include "../Mx/Component/Camera/MxCamera.h"
#include "../Mx/Log/MxLog.h"
#include "../Mx/Window/MxWindow.h"
#include "PlayerAdapter.h"

namespace Scripts {
MX_IMPLEMENT_RTTI(PlayerScript, Script);

void PlayerScript::start() {
mAdapter = mGameObject->getComponent<PlayerScript>;
}
void PlayerScript::update() {
mAdapter->move(getDir());

mAdapter->attack();
}

void PlayerScript::fixedUpdate() {
}

Vector2f PlayerScript::getDir() {
auto dir = dirFromKeyboard() + dirFromGamepad();
return !(dir.length() > 0.0f) ? Vector2f::Zero : dir.normalize();
}

Vector2f PlayerScript::dirFromGamepad() {
auto dir = Input::Get()->getGamepadLeftStickAxis();
return !(dir.length() > 0.0f) ? Vector2f::Zero : dir.normalize();
}

Vector2f PlayerScript::dirFromKeyboard() {
Vector2f dir;
if (Input::Get()->isButtonHold(ButtonCode::W)) {
dir += Vector2f::Up;
}
if (Input::Get()->isButtonHold(ButtonCode::A)) {
dir += Vector2f::Left;
}
if (Input::Get()->isButtonHold(ButtonCode::S)) {
dir += Vector2f::Down;
}
if (Input::Get()->isButtonHold(ButtonCode::D)) {
dir += Vector2f::Right;
}
return dir.length() > 0.0f ? dir.normalize() : Vector2f::Zero;
}
}
33 changes: 33 additions & 0 deletions Scripts/Player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include "../Mx/Component/Script/MxScript.h"
#include "../Mx/GameObject/MxGameObject.h"
#include "../Mx/Math/MxVector.h"
#include "../Mx/Utils/MxEvent.h"

namespace Scripts {
using namespace Mix;

class PlayerAdapter;

class PlayerScript : public Script {
MX_DECLARE_RTTI;
public:
~PlayerScript() = default;

private:
void start() override;

void update() override;

void fixedUpdate() override;

Vector2f getDir();

Vector2f dirFromGamepad();

Vector2f dirFromKeyboard();

SceneObjectHandle<PlayerAdapter> mAdapter;

};
}
40 changes: 40 additions & 0 deletions Scripts/PlayerAdapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "Player.h"
#include "../Mx/Input/MxInput.h"
#include "../Mx/Component/Camera/MxCamera.h"
#include "../Mx/Log/MxLog.h"
#include "../Mx/Window/MxWindow.h"
#include "PlayerAdapter.h"

namespace Scripts {
MX_IMPLEMENT_RTTI(PlayerAdapter, Script);

void PlayerAdapter::move(const Vector2f& _dir) {
auto moveDir = _dir.length() > 0.0f ? _dir.normalize() : Vector2f::Zero;

mSmoothMove = Vector2f::Lerp(mSmoothMove, moveDir * mMoveSpeed, mAccelerate);
mSmoothMove = Vector2f::Lerp(mSmoothMove, Vector2f::Zero, mDecelerate);
transform()->translate(mSmoothMove.x, mSmoothMove.y, 0.0f, Space::World);

int hDir = moveDir.x > 0.0f ? -1 : (moveDir.x < 0.0f ? 1 : 0);

}

void PlayerAdapter::attack() {
//auto weapon = mGameObject->getComponent<...>();
//if (weapon != nullptr)
// weapon->attack();
}

void PlayerAdapter::setMoveSpeed(float _speed) {
mMoveSpeed = _speed;
}

void PlayerAdapter::awake() {
}

void PlayerAdapter::update() {
}

void PlayerAdapter::fixedUpdate() {
}
}
37 changes: 37 additions & 0 deletions Scripts/PlayerAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once
#include "../Mx/Component/Script/MxScript.h"
#include "../Mx/GameObject/MxGameObject.h"
#include "../Mx/Math/MxVector.h"
#include "../Mx/Utils/MxEvent.h"

namespace Scripts {
using namespace Mix;

class PlayerAdapter : public Script {
MX_DECLARE_RTTI;
public:
~PlayerAdapter() = default;

void move(const Vector2f& _dir);

void attack();

void setMoveSpeed(float _speed);

private:
void awake() override;

void start() override;

void update() override;

void fixedUpdate() override;

float mMoveSpeed = 0.5f;
float mAccelerate = 0.5f;
float mDecelerate = 0.5f;
Vector2f mSmoothMove;


};
}

0 comments on commit 0e4e383

Please sign in to comment.