Skip to content

Commit

Permalink
feat: object attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadecraft committed Mar 23, 2024
1 parent 473f3f8 commit ba57501
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 26 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_executable(PlatScifi
src/worldobjects/stationaryimage.h
src/worldobjects/player.h
src/worldobjects/staticcollider.h
src/worldobjects/spike.h
)
target_link_libraries(PlatScifi PRIVATE sfml-graphics)
target_compile_features(PlatScifi PRIVATE cxx_std_17)
Expand All @@ -48,6 +49,8 @@ install(TARGETS PlatScifi)
set(ASSETS
essential.txt
tiv_logo.png
grass1.png
spike.png
)

foreach(ASSET ${ASSETS})
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# PlatScifi

A platformer game by the tivect team

## Building:

CMake is required, and SFML will automatically be downloaded.

Check out `README_BuildTemplate.md` for more specific instructions

## Controls:

- `[a]`/`[d]` - move left/right

- `[w]`/`[space]` - jump

- `[s]` - fall faster

- `[r]` - reset to the spawn point

- `[p]` - spawn a bouncing ball or other object (for testing)
Binary file added src/assets/grass1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/spike.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "worldobjects/stationaryimage.h"
#include "worldobjects/player.h"
#include "worldobjects/staticcollider.h"
#include "worldobjects/spike.h"

// SFML Demo
int main() {
Expand All @@ -29,10 +30,7 @@ int main() {
Player* player = new Player(5, 10);
gameState.spawnObject(player);

// Debug: create a red cube, blue cube, image, etc.
gameState.spawnObject(new RedCube(0, 0));
gameState.spawnObject(new BlueCube(0, 0));
gameState.spawnObject(new StationaryImage(6, 6, 2, 2, "assets/tiv_logo.png"));
// Debug: create a basic level
// Bottom colliders
gameState.spawnObject(new StaticCollider(3, 25, 6, 2));
gameState.spawnObject(new StaticCollider(9, 27, 10, 2));
Expand All @@ -42,6 +40,8 @@ int main() {
gameState.spawnObject(new StaticCollider(25, 0, 2, 19));
// Extra blocks
gameState.spawnObject(new StaticCollider(23, 29, 44, 19));
// Spike
gameState.spawnObject(new Spike(28, 28));

// Main game loop
while (window.isOpen()) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Renderer::Renderer(sf::RenderWindow& window, AssetHandler& assetHandler)
: window(window), assetHandler(assetHandler) { }

void Renderer::renderFromData(RenderData& data) {
void Renderer::renderFromData(RenderData data) {
// TODO: implement other types (like images)
if (data.type == RenderType::Rectangle) {
// Rectangle
Expand Down
2 changes: 1 addition & 1 deletion src/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Renderer {
Renderer(sf::RenderWindow& window, AssetHandler& assetHandler);

// Render from a RenderData object
void renderFromData(RenderData& data);
void renderFromData(RenderData data);

// Set the camera
void setCamera(double newWorldCameraCenterX, double newWorldCameraCenterY, double percentEasing = 0.0);
Expand Down
4 changes: 4 additions & 0 deletions src/worldobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ double WorldObject::getHeight() {
return height;
}

bool WorldObject::hasAttribute(ObjectAttribute attribute) {
return objectAttributes.find(attribute) != objectAttributes.end();
}

RenderData WorldObject::getRenderData() {
return {
RenderType::Rectangle,
Expand Down
11 changes: 11 additions & 0 deletions src/worldobject.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <vector>
#include <set>
#include "renderdata.h"
#include "worldstate.h"

Expand All @@ -10,6 +11,12 @@ enum class UpdateResult {
Destroy
};

// The attributes that an object can have
enum class ObjectAttribute {
Collision,
Deadly
};

// Stores a world object
class WorldObject {
protected:
Expand All @@ -18,6 +25,8 @@ class WorldObject {
double locy;
double width;
double height;
// TODO: enum instead of stringly typed?
std::set<ObjectAttribute> objectAttributes;

public:
// Create the WorldObject
Expand All @@ -34,6 +43,8 @@ class WorldObject {
double getWidth();
double getHeight();

bool hasAttribute(ObjectAttribute attribute);

// TODO: get whether a rectangle is inside of this object

// Return the results to render
Expand Down
1 change: 1 addition & 0 deletions src/worldobjects/bluecube.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class BlueCube : public WorldObject {
BlueCube(double spawnx, double spawny) : WorldObject() {
locx = spawnx;
locy = spawny;
objectAttributes.insert(ObjectAttribute::Collision);
}

// Override the update function
Expand Down
47 changes: 29 additions & 18 deletions src/worldobjects/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,37 @@ class Player : public WorldObject {
// todo: when hitting multiple?
for (WorldObject* object : objects) {
if (object == this) continue;
// X movement pushout
// todo: better way of doing the 0.1 thing
if (locy + height - 0.1 >= object->getLocy() && locy + 0.1 < object->getLocy() + object->getHeight()) {
// Within the y
if (locx + width > object->getLocx() && locx < object->getLocx() + object->getWidth()) {
// Side wall
locx -= velx;
velx = 0;
bool collided = false;
if (object->hasAttribute(ObjectAttribute::Collision)) {
// X movement pushout
// todo: better way of doing the 0.1 thing
if (locy + height - 0.1 >= object->getLocy() && locy + 0.1 < object->getLocy() + object->getHeight()) {
// Within the y
if (locx + width > object->getLocx() && locx < object->getLocx() + object->getWidth()) {
// Side wall
locx -= velx;
velx = 0;
collided = true;
}
}
}
if (locx + width >= object->getLocx() && locx < object->getLocx() + object->getWidth()) {
// Within the x
if (locy + height > object->getLocy() && locy < object->getLocy() + object->getHeight()) {
// Floor/ceiling
locy -= vely;
vely = 0;
// todo: should not be on ground on a bottom corner
onGround = true;
if (locx + width >= object->getLocx() && locx < object->getLocx() + object->getWidth()) {
// Within the x
if (locy + height > object->getLocy() && locy < object->getLocy() + object->getHeight()) {
// Floor/ceiling
locy -= vely;
vely = 0;
collided = true;
// todo: should not be on ground on a bottom corner
onGround = true;
}
}
}
// Collision stuff
if (collided && object->hasAttribute(ObjectAttribute::Deadly)) {
// Die
// todo: impl
teleport(5, 10);
}
}
return UpdateResult::None;
}
Expand All @@ -91,7 +102,7 @@ class Player : public WorldObject {
locy,
width,
height,
{ 255, 0, 0 },
{ 0, 255, 0 },
""
};
}
Expand Down
41 changes: 41 additions & 0 deletions src/worldobjects/spike.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include "../worldobject.h"

// Stores a stationary image at a point in space
class Spike : public WorldObject {
private:
// Frame oscillations
long frameCount = 0;

public:
// Constructor: provide the name of the asset from assets (ex. "assets/tiv_logo.png")
Spike(double spawnx, double spawny) : WorldObject() {
locx = spawnx;
locy = spawny;
this->width = 1;
this->height = 1;
objectAttributes.insert(ObjectAttribute::Collision);
objectAttributes.insert(ObjectAttribute::Deadly);
}

// Override update: do nothing
UpdateResult update(WorldState& worldState, std::vector<WorldObject*>& objects) {
return UpdateResult::None;
}

// Override rendering
RenderData getRenderData() {
frameCount++;
return {
RenderType::Image,
coordType,
locx,
locy - sin(frameCount / 400) * 0.3,
width,
height + sin(frameCount / 400) * 0.3,
{ 255, 0, 0 },
"assets/spike.png"
};
}
};
5 changes: 3 additions & 2 deletions src/worldobjects/staticcollider.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class StaticCollider : public WorldObject {
locy = spawny;
this->width = width;
this->height = height;
objectAttributes.insert(ObjectAttribute::Collision);
}

// Override update: gravity
Expand All @@ -22,14 +23,14 @@ class StaticCollider : public WorldObject {
RenderData getRenderData() {
// TODO: image/animations
return {
RenderType::Rectangle,
RenderType::Image,
coordType,
locx,
locy,
width,
height,
{ 14, 92, 81 },
""
"assets/grass1.png"
};
}
};

0 comments on commit ba57501

Please sign in to comment.