Integrated XBox360 controller support for camera and player movement.

This commit is contained in:
2025-03-18 00:34:08 -04:00
parent 0b9c5eb449
commit e547dd0045
7 changed files with 36 additions and 5 deletions

View File

@@ -47,6 +47,11 @@ CPMAddPackage(
URL https://git.redacted.cc/josh/j3ml/archive/3.4.5.zip
)
CPMAddPackage(
NAME jstick
URL https://git.redacted.cc/josh/jstick/archive/Prerelease-2.zip
)
CPMAddPackage(
NAME ReWindow
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-32.zip

View File

@@ -13,6 +13,7 @@ endif()
target_include_directories(CaveClient PUBLIC
${CaveCore_SOURCE_DIR}/include
# ${jstick_SOURCE_DIR}/include
${J3ML_SOURCE_DIR}/include
${JGL_SOURCE_DIR}/include
${JUI_SOURCE_DIR}/include

View File

@@ -86,6 +86,7 @@ namespace CaveGame::Client
Matrix4x4 Transform() const;
void Move(const Vector2& v);
/// Moves the camera to the left at a given rate times the base camera speed.
void MoveLeft(float rate = 1.f);
/// Moves the camera to the right at a given rate times the base camera speed.

View File

@@ -1,5 +1,6 @@
#include <Client/Camera2D.hpp>
#include <ReWindow/InputService.h>
#include <jstick.hpp>
namespace CaveGame::Client
{
@@ -11,6 +12,12 @@ namespace CaveGame::Client
{
// TODO: implement freecam panning via mouse.
Vector2 m = jstick::GetLeftThumbstickAxisNormalized();
if (m.Magnitude() > 0.1f)
Move(m*elapsed);
if (InputService::IsKeyDown(Keys::LeftArrow))
MoveLeft(elapsed);
if (InputService::IsKeyDown(Keys::RightArrow))
@@ -102,6 +109,11 @@ namespace CaveGame::Client
target -= Vector2(move_speed * rate, 0);
}
void Camera2D::Move(const Vector2& velocity)
{
target += velocity * move_speed;
}
void Camera2D::MoveRight(float rate) {
target += Vector2(move_speed * rate, 0);
}

View File

@@ -3,7 +3,7 @@
#include <Core/Entity.hpp>
#include <JGL/JGL.h>
#include <ReWindow/InputService.h>
#include <jstick.hpp>
// TODO: Move this shit to Client/Player.cpp
@@ -52,15 +52,21 @@ void CaveGame::Core::Player::Update(float elapsed) {
anim_timer += elapsed;
walking = false;
if (InputService::IsKeyDown(Keys::A))
Vector2 dpad = jstick::GetDPadAxisNormalized();
if (InputService::IsKeyDown(Keys::A) || dpad.x <= -0.5f)
WalkLeft(elapsed);
if (InputService::IsKeyDown(Keys::D))
if (InputService::IsKeyDown(Keys::D) || dpad.x >= +0.5f)
WalkRight(elapsed);
if (InputService::IsKeyDown(Keys::W))
if (InputService::IsKeyDown(Keys::W) || dpad.y <= -0.5f)
Jump(elapsed);
//if (InputService::IsKeyDown(Keys::S))
//Accelerate({0, -1});
}

View File

@@ -7,6 +7,7 @@
#include <Core/Player.hpp>
#include <ReWindow/InputService.h>
#include <Core/Macros.hpp>
#include <jstick.hpp>
namespace CaveGame::ClientApp
{
@@ -90,6 +91,8 @@ namespace CaveGame::ClientApp
void CaveGameWindow::Run()
{
int js = jstick::Connect();
//this->SetRenderer(RenderingAPI::OPENGL);
bool success = this->Open();
@@ -223,6 +226,8 @@ namespace CaveGame::ClientApp
void CaveGameWindow::Update(float elapsed)
{
jstick::ReadEventLoop();
// Update floating windows.
wm->Update(elapsed);

View File

@@ -19,5 +19,6 @@ target_include_directories(CaveCore PUBLIC ${mcolor_SOURCE_DIR}/include)
target_include_directories(CaveCore PUBLIC ${jjx_SOURCE_DIR}/include)
target_include_directories(CaveCore PUBLIC ${Sockets_SOURCE_DIR}/include)
target_include_directories(CaveCore PUBLIC ${JGL_SOURCE_DIR}/include)
target_include_directories(CaveCore PUBLIC ${jstick_SOURCE_DIR}/include)
target_link_libraries(CaveCore PUBLIC J3ML JGL jjx mcolor Sockets)
target_link_libraries(CaveCore PUBLIC J3ML JGL jjx mcolor Sockets jstick)