Skip to content

Commit

Permalink
feat: player and basic input
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadecraft committed Mar 21, 2024
1 parent 54c53ee commit a80deee
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ FetchContent_Declare(SFML
FetchContent_MakeAvailable(SFML)

# Add all source files
# TODO: glob?
add_executable(PlatScifi
src/main.cpp
src/renderdata.h
Expand All @@ -25,6 +26,7 @@ add_executable(PlatScifi
src/worldobjects/redcube.h
src/worldobjects/bluecube.h
src/worldobjects/stationaryimage.h
src/worldobjects/player.h
)
target_link_libraries(PlatScifi PRIVATE sfml-graphics)
target_compile_features(PlatScifi PRIVATE cxx_std_17)
Expand Down
36 changes: 34 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <set>
#include "assethandler.h"
#include "renderer.h"
#include "renderdata.h"
Expand All @@ -9,6 +9,7 @@
#include "worldobjects/redcube.h" // Debugging
#include "worldobjects/bluecube.h"
#include "worldobjects/stationaryimage.h"
#include "worldobjects/player.h"

// SFML Demo
int main() {
Expand All @@ -21,18 +22,49 @@ int main() {
AssetHandler assetHandler;
Renderer renderer(window, assetHandler);
WorldState worldState;
std::set<sf::Keyboard::Key> keysPressed;

// Main world objects
Player* player = new Player(4, 10);
worldState.spawnObject(player);

// Debug: create a red cube, blue cube, and image
worldState.spawnObject(new RedCube(0, 0));
worldState.spawnObject(new BlueCube());
worldState.spawnObject(new BlueCube(0, 0));
worldState.spawnObject(new StationaryImage(6, 6, 2, 2, "assets/tiv_logo.png"));

// Main game loop
while (window.isOpen()) {
// Poll events
for (auto event = sf::Event{}; window.pollEvent(event);) {
if (event.type == sf::Event::Closed) {
// Close window
window.close();
} else if (event.type == sf::Event::KeyPressed) {
keysPressed.insert(event.key.code);
} else if (event.type == sf::Event::KeyReleased) {
keysPressed.erase(event.key.code);
}
}

// Use input
if (keysPressed.find(sf::Keyboard::A) != keysPressed.end()) {
player->move(-0.2, 0);
}
if (keysPressed.find(sf::Keyboard::D) != keysPressed.end()) {
player->move(0.2, 0);
}
if (keysPressed.find(sf::Keyboard::W) != keysPressed.end()) {
player->move(0, -0.2);
}
if (keysPressed.find(sf::Keyboard::S) != keysPressed.end()) {
player->move(0, 0.2);
}
if (keysPressed.find(sf::Keyboard::Space) != keysPressed.end()) {
worldState.spawnObject(new BlueCube(player->getLocx(), player->getLocy()));
keysPressed.erase(sf::Keyboard::Space);
}

// Update the world
worldState.update();

Expand Down
5 changes: 4 additions & 1 deletion src/worldobjects/bluecube.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class BlueCube : public WorldObject {
double vely = 0.15;

public:
BlueCube() : WorldObject() {}
BlueCube(double spawnx, double spawny) : WorldObject() {
locx = spawnx;
locy = spawny;
}

// Override the update function
UpdateResult update() {
Expand Down
48 changes: 48 additions & 0 deletions src/worldobjects/player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include "../worldobject.h"

// The data for the player
class Player : public WorldObject {
public:
// Constructor
Player(double spawnx, double spawny) : WorldObject() {
locx = spawnx;
locy = spawny;
}

// Input: movement
// TODO: accelerate instead of move
void move(double deltax, double deltay) {
locx += deltax;
locy += deltay;
}

double getLocx() {
return locx;
}

double getLocy() {
return locy;
}

// Override update: do nothing (yet)
UpdateResult update() {
return UpdateResult::None;
}

// Override rendering
RenderData getRenderData() {
// TODO: image/animations
return {
RenderType::Rectangle,
coordType,
locx,
locy,
width,
height,
{ 255, 0, 0 },
""
};
}
};

0 comments on commit a80deee

Please sign in to comment.