forked from hitsz-ldjam/MixEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5f34e0
commit 0e4e383
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
}; | ||
} |