Skip to content

Commit

Permalink
Merge pull request #3 from tivect/collisionfix
Browse files Browse the repository at this point in the history
Collisionfix
  • Loading branch information
Cadecraft authored Apr 21, 2024
2 parents e862bfc + 2e3aeb1 commit c3eade7
Show file tree
Hide file tree
Showing 44 changed files with 830 additions and 165 deletions.
40 changes: 26 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ add_executable(PlatScifi
src/renderer.h
src/worldstate.cpp
src/worldstate.h
src/gamestate.cpp
src/gamestate.h
src/worldspawner.cpp
src/worldspawner.h
src/worldobjectincludes.h
Expand All @@ -38,6 +36,10 @@ add_executable(PlatScifi
src/worldobjects/staticdeadly.h
src/worldobjects/leveltp.h
src/worldobjects/spike.h
src/worldobjects/animal.cpp
src/worldobjects/player.cpp
src/gamestate.cpp
src/gamestate.h
)
target_link_libraries(PlatScifi PRIVATE sfml-graphics)
target_compile_features(PlatScifi PRIVATE cxx_std_17)
Expand All @@ -55,22 +57,32 @@ install(TARGETS PlatScifi)
# Copy over the assets folder
# TODO: use a GLOB
set(ASSETS
essential.txt
tiv_logo.png
grass1.png
yellowpad.png
spike.png
level_0.csv
level_1.csv
level_2.csv
level_design.csv
essential.txt
tiv_logo.png
grass1.png
sand.png
yellowpad.png
spike.png
level_0.csv
level_1.csv
level_2.csv
level_design.csv
LiberationSans-Regular.ttf
enemy_bird_0.png
enemy_bird_1.png
enemy_fipa_0.png
enemy_replicator_0.png
enemy_replicator_1.png
enemy_replicator_2.png
enemy_addax_0.png
enemy_addax_1.png
)

foreach(ASSET ${ASSETS})
# This makes files, but corrupts image files
# file(GENERATE OUTPUT "bin/Debug/assets/${ASSET}" INPUT "src/assets/${ASSET}")
# file(GENERATE OUTPUT "bin/Release/assets/${ASSET}" INPUT "src/assets/${ASSET}")
# This copies the asset into the build assets folder:
file(COPY "src/assets/${ASSET}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/bin/Debug/assets/")
file(COPY "src/assets/${ASSET}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/bin/Release/assets/")
# This correctly copies the asset into the build assets folder:
file(COPY "src/assets/${ASSET}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/bin/Debug/assets/")
file(COPY "src/assets/${ASSET}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/bin/Release/assets/")
endforeach()
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# PlatScifi

A platformer game by the tivect team
A simple but challenging platformer game by the tivect team (with some silly graphics).

The game isn't finished--but it will be one day.

## Building:

Expand Down
14 changes: 14 additions & 0 deletions src/assethandler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "assethandler.h"
#include <iostream> // DEBUGGING
// ONLY

sf::Image AssetHandler::getImage(std::string path) {
// Return if image already exists
Expand All @@ -14,3 +16,15 @@ sf::Image AssetHandler::getImage(std::string path) {
images[path] = img;
return img;
}

// Get a reference to the font, not a copy
sf::Font& AssetHandler::getMainFont() {
if (mainFontLoaded) return mainFont;
if (!mainFont.loadFromFile("assets/LiberationSans-Regular.ttf")) {
// Error
std::cout << "ERR: Failed to load 'assets/LiberationSans-Regular.ttf'" << std::endl;
// TODO: handle
}
mainFontLoaded = true;
return mainFont;
}
4 changes: 4 additions & 0 deletions src/assethandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
class AssetHandler {
private:
std::map<std::string, sf::Image> images;
bool mainFontLoaded = false;
sf::Font mainFont;

public:
// Get the image from a path (ex. "assets/tiv_logo.png")
sf::Image getImage(std::string path);
// Get the main font
sf::Font& getMainFont();
};
Binary file added src/assets/LiberationSans-Regular.ttf
Binary file not shown.
Binary file added src/assets/enemy_addax_0.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/enemy_addax_1.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/enemy_bird_0.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/enemy_bird_1.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/enemy_fipa_0.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/enemy_replicator_0.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/enemy_replicator_1.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/enemy_replicator_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/assets/level_0.csv
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Spike, 28, 28
// Static deadly section
StaticDeadly, 33, 28, 5, 2
// Ending teleporter
LevelTp, 55, 28
Level, 55, 28
25 changes: 7 additions & 18 deletions src/assets/level_1.csv
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
// First Level (level_1)
// First level (level_1)
// Go to the next level at the end
// For debugging, change this to any level; by default, should be level_2
nextLevelName, level_2
// Bottom colliders
StaticCollider, 3, 25, 6, 2
//StaticCollider, 9, 27, 10, 2
StaticCollider, 19, 25, 6, 2
// Vertical colliders
StaticCollider, 0, 0, 2, 25
StaticCollider, 25, 0, 2, 19
// Extra blocks
StaticCollider, 23, 29, 44, 19
// Spike
Spike, 28, 28
// Static deadly section
StaticDeadly, 33, 28, 5, 2
// Ending teleporter
LevelTp, 55, 28
// Moving Platform
MovingCollider, 8, 32, 5, 2, 300, 20, 32
// Bottom grass
StaticCollider, 2, 16, 40, 4
// Next level
LevelTp, 38, 15
13 changes: 7 additions & 6 deletions src/assets/level_2.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Second level (level_2)
// Cycle back to the start
nextLevelName, level_1
// Bottom collider
StaticCollider, 2, 30, 30, 2
// Platforms (level_2)
nextLevelName, level_3
// Grass has a hole in it
StaticCollider, 2, 16, 40, 4
MovingCollider, 42, 16, 8, 2, 300, 74, 16
StaticCollider, 82, 16, 40, 4
// Next level
LevelTp, 50, 29
LevelTp, 100, 15
10 changes: 10 additions & 0 deletions src/assets/level_3.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Enemies (level_3)
nextLevelName, level_4
// Uphill
StaticCollider, 2, 16, 28, 4
StaticCollider, 30, 12, 30, 8
StaticCollider, 60, 8, 40, 8
// Enemies
Animal, fipa, 55, 6
// Next level
LevelTp, 80, 7
27 changes: 27 additions & 0 deletions src/assets/level_4.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Insane Level (level_4)
nextLevelName, level_1
// Bottom colliders
StaticCollider, 3, 25, 6, 2
//StaticCollider, 9, 27, 10, 2
StaticCollider, 19, 25, 6, 2
// Vertical colliders
StaticCollider, 0, 0, 2, 25
StaticCollider, 25, 0, 2, 19
// Main large bottom block
StaticCollider, 23, 29, 80, 19
// Rightmost barrier
StaticCollider, 103, 26, 2, 3
// Ending teleporter
LevelTp, 100, 28
// Spike
Spike, 28, 28
// Static deadly section
StaticDeadly, 33, 28, 5, 2
// Moving Platforms
MovingCollider, 8, 32, 5, 2, 300, 20, 32
MovingCollider, 40, 24, 5, 2, 300, 40, 15
MovingCollider, 47, 15, 5, 2, 300, 45, 15
// Animals
Animal, bird, 50, 15
Animal, fipa, 55, 15
Animal, replicator, 58, 15
Binary file added src/assets/sand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
const bool LEVEL_DESIGN_MODE = false;

// REM info
const std::string RECENT_VERSION = "0.1.0";
const std::string EDIT_DATE = "2024/03/31";
const std::string RECENT_VERSION = "0.1.1";
const std::string EDIT_DATE = "2024/04/08";
69 changes: 62 additions & 7 deletions src/gamestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,52 @@ void GameState::spawnObject(WorldObject* object) {
}

UpdateResult GameState::update() {
// Update the collision matrix with every world object
collisionMatrix.clear();
for (WorldObject* object : objects) {
UpdateResult updateResult = object->update(worldState, objects);
collisionMatrix.addObject(object);
}

// Update world objects
std::vector<WorldObject*> newToSpawn;
for (std::vector<WorldObject*>::iterator it = objects.begin(); it != objects.end();) {
UpdateResult updateResult = (*it)->update(*this);
if (updateResult == UpdateResult::NextLevel) {
// TODO: improve this
return UpdateResult::NextLevel;
} else if (updateResult == UpdateResult::DieReset) {
// Player died
return UpdateResult::DieReset;
} else if (updateResult == UpdateResult::ReplicateAndDestroy) {
// Replicate it (spawn two replicators)
Animal* animal1 = new Animal(
"replicator", (*it)->getLocx(), (*it)->getLocy()
);
animal1->teleport(animal1->getLocx() - 1.5, animal1->getLocy()); // -0.7
animal1->setDimensions((*it)->getWidth() * 0.8, (*it)->getHeight() * 0.8);
newToSpawn.push_back(animal1);
Animal* animal2 = new Animal(
"replicator", (*it)->getLocx(), (*it)->getLocy()
);
animal2->teleport(animal2->getLocx() + 1.5, animal2->getLocy());
animal2->setDimensions((*it)->getWidth() * 0.8, (*it)->getHeight() * 0.8);
animal2->reverseDirection();
newToSpawn.push_back(animal2);
// Destroy the previous enemy
it = objects.erase(it);
} else {
// Move on
it++;
}
}
for (WorldObject* newObject : newToSpawn) objects.push_back(newObject);
// Update UI messages
for (std::vector<UIMessage>::iterator it = uiMessages.begin(); it != uiMessages.end();) {
if (it->update()) {
// Delete the message
it = uiMessages.erase(it);
} else {
it++;
}
}
return UpdateResult::None;
Expand All @@ -23,17 +64,31 @@ std::string GameState::getNextLevelName() {
return nextLevelName;
}

WorldState& GameState::getWorldState() {
return worldState;
}

WorldCollisionMatrix& GameState::getCollisionMatrix() {
return collisionMatrix;
}

void GameState::updateLevelNames(std::string newLevelName, std::string newNextLevelName) {
levelName = newLevelName;
nextLevelName = newNextLevelName;
// Add the message
// TODO: use level display names
addUIMessage("Teleported to " + newLevelName, { 0, 160, 0 });
}

std::vector<WorldObject*>::iterator GameState::objectsBegin() {
return objects.begin();
}

std::vector<WorldObject*>::iterator GameState::objectsEnd() {
return objects.end();
void GameState::addUIMessage(std::string message, Color color, int fontSize, long durationFrames, bool debug) {
// TODO: do not add debug messages in a release build
uiMessages.push_back({
message,
color,
fontSize,
durationFrames
});
if (uiMessages.size() > 5) uiMessages.erase(uiMessages.begin());
}

void GameState::clear() {
Expand Down
19 changes: 15 additions & 4 deletions src/gamestate.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

#include <vector>
#include "worldobject.h"
#include "worldobjectincludes.h"
#include "worldcollisionmatrix.h"
#include "worldstate.h"
#include "uimessage.h"

// Store the full state of the world and all objects
class GameState {
private:
WorldState worldState;
WorldCollisionMatrix collisionMatrix;
std::string levelName;
std::string nextLevelName;
std::vector<WorldObject*> objects;

public:
// All world objects (public)
std::vector<WorldObject*> objects;
// All UI messages (public)
std::vector<UIMessage> uiMessages;

// Spawn an object
void spawnObject(WorldObject* object);
// Update
Expand All @@ -21,11 +29,14 @@ class GameState {
std::string getLevelName();
// Get the next level name
std::string getNextLevelName();
// Get the current world state
WorldState& getWorldState();
// Get the current collision matrix
WorldCollisionMatrix& getCollisionMatrix();
// Update the current level name
void updateLevelNames(std::string newLevelName, std::string newNextLevelName);
// Get all objects
std::vector<WorldObject*>::iterator objectsBegin();
std::vector<WorldObject*>::iterator objectsEnd();
// Add a UI message
void addUIMessage(std::string message, Color color, int fontSize = 14, long durationFrames = 150, bool debug = false);
// Clear all objects from the world, aside from Persisting objects
void clear();
// Destructor to free memory
Expand Down
Loading

0 comments on commit c3eade7

Please sign in to comment.