basic movement

This commit is contained in:
2026-03-14 22:27:54 +01:00
parent b19f595daf
commit 2983b919cd
24 changed files with 368 additions and 57 deletions

View File

@@ -0,0 +1,31 @@
#include <game/agame/background.hpp>
#include <window/window.hpp>
namespace Game::AGame {
void Background::start() {
mZIndex = -1; // Ensure background renders behind other entities
mTex->setTiled(true); // Set the background texture to be tiled
mTiledScale = 0.5f;
int w, h;
SDL_GetWindowSizeInPixels(Window::Window::getSDLWindowBackend(), &w, &h);
mTransform.scaleX *= 10.f;
mTransform.scaleY *= 10.f;
mTransform.x -= mTex->getWidth() * mTransform.adjustedScaleX() / 2.f;
mTransform.y -= mTex->getHeight() * mTransform.adjustedScaleY() / 2.f;
LOG("W: " << w << " H: " << h);
mSound.~Sound();
}
void Background::update(float deltaTime) {
if (!mIsActive) return;
//mTransform.rotation += 1.f; // Rotate clockwise for testing
//mTransform.scaleX = 1.f + 1.f * std::sin(RUNNING_TIME() / 0.5f); // Pulsate scale for testing
//mTransform.scaleY = 1.f + 0.5f * std::cos(RUNNING_TIME() / 0.5f); // Pulsate scale for testing
//Object::Camera::getInstance().move(1.f, 0.f);
}
}

View File

@@ -0,0 +1,41 @@
#include <game/agame/camcontroller.hpp>
#include <window/window.hpp>
#include <cmath>
#include <object/camera.hpp>
#include <state/gamestate.hpp>
namespace Game::AGame {
void CamController::start() {
mTex = nullptr; // No texture
SDL_GetWindowSizeInPixels(Window::Window::getSDLWindowBackend(), &mScreenW, &mScreenH);
}
void CamController::update(float deltaTime) {
if (!mIsActive) return;
Object::Entity* player = Game::State::GameState::getInstance().getEntityByName("Player"); // We get the pointer every frame, otherwise we might get screwed by vector reallocs
if (!player) return; // If the player doesn't exist, don't do anything
float playerX = player->getTransform()->x;
float playerY = player->getTransform()->y;
float camX, camY;
Object::Camera::getInstance().getPosition(camX, camY);
// Apply tolerance from the edges of the screen, so the camera doesn't immediately start moving when the player moves a little bit
float leftBound = camX - mScreenW / 2.f + mEdgeTolerance;
float rightBound = camX + mScreenW / 2.f - mEdgeTolerance;
float topBound = camY - mScreenH / 2.f + mEdgeTolerance;
float bottomBound = camY + mScreenH / 2.f - mEdgeTolerance;
if (playerX < leftBound) {
Object::Camera::getInstance().move(-mSpeed * deltaTime, 0.f);
} else if (playerX > rightBound) {
Object::Camera::getInstance().move(mSpeed * deltaTime, 0.f);
}
if (playerY < topBound) {
Object::Camera::getInstance().move(0.f, -mSpeed * deltaTime);
} else if (playerY > bottomBound) {
Object::Camera::getInstance().move(0.f, mSpeed * deltaTime);
}
}
}

View File

@@ -5,26 +5,33 @@
namespace Game::AGame {
void Player::start() {
mSound = Object::Sound("../resources/example.wav", Object::Format::WAV);
mSound.play();
//mSound.play();
int w, h;
SDL_GetWindowSizeInPixels(Window::Window::getSDLWindowBackend(), &w, &h);
mTransform.x = w / 2.f - (mTex->getWidth() / 2.f * mTransform.scaleX * 0.25f); // Start in the middle of the screen
mTransform.y = h / 2.f - (mTex->getHeight() / 2.f * mTransform.scaleY * 0.25f);
mTransform.rotation = 0.f;
mTransform.x -= mTex->getWidth() * mTransform.adjustedScaleX() / 2.f;
mTransform.y -= mTex->getHeight() * mTransform.adjustedScaleY() / 2.f;
LOG("W: " << w << " H: " << h);
mSound.~Sound();
}
void Player::update(float deltaTime) {
if (!mIsActive) return;
//LOG("Updated Player");
//mTransform.x += 1.f; // Move right at a constant speed for testing
mTransform.rotation += 1.f; // Rotate clockwise for testing
//LOG(mName << " position: " << mTransform.x << ' ' << mTransform.y);
//LOG("DeltaTime: " << deltaTime);
mTransform.scaleX = 1.f + 1.f * std::sin(RUNNING_TIME() / 0.5f); // Pulsate scale for testing
mTransform.scaleY = 1.f + 0.5f * std::cos(RUNNING_TIME() / 0.5f); // Pulsate scale for testing
//mTransform.rotation += 1.f; // Rotate clockwise for testing
//mTransform.scaleX = 1.f + 1.f * std::sin(RUNNING_TIME() / 0.5f); // Pulsate scale for testing
//mTransform.scaleY = 1.f + 0.5f * std::cos(RUNNING_TIME() / 0.5f); // Pulsate scale for testing
//Object::Camera::getInstance().move(1.f, 0.f);
// Simple movement
const bool* state = SDL_GetKeyboardState(nullptr);
if (state[SDL_SCANCODE_W]) mTransform.y -= mSpeed * deltaTime;
if (state[SDL_SCANCODE_S]) mTransform.y += mSpeed * deltaTime;
if (state[SDL_SCANCODE_A]) mTransform.x -= mSpeed * deltaTime;
if (state[SDL_SCANCODE_D]) mTransform.x += mSpeed * deltaTime;
}
}