Skip to content

Commit

Permalink
Turtle shell interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
feresr committed May 18, 2020
1 parent 3e6bf3c commit ed3cb8b
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 34 deletions.
16 changes: 15 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ find_package(SDL2_image REQUIRED)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")

add_executable(smb src/Main.cpp src/Game.cpp src/ecs/ecs.cpp src/systems/RenderSystem.cpp src/systems/MapSystem.cpp src/systems/EditorSystem.cpp src/TextureManager.cpp src/systems/PhysicsSystem.cpp src/systems/AnimationSystem.cpp src/systems/PlayerSystem.cpp src/systems/TileSystem.cpp src/systems/CallbackSystem.cpp src/systems/EnemySystem.cpp)
add_executable(smb
src/Main.cpp
src/Game.cpp
src/ecs/ecs.cpp
src/systems/RenderSystem.cpp
src/systems/MapSystem.cpp
src/systems/EditorSystem.cpp
src/TextureManager.cpp
src/systems/PhysicsSystem.cpp
src/systems/AnimationSystem.cpp
src/systems/PlayerSystem.cpp
src/systems/TileSystem.cpp
src/systems/CallbackSystem.cpp
src/systems/EnemySystem.cpp
src/AABB.cpp)

set_property(TARGET smb PROPERTY CXX_STANDARD 17)
set_target_properties(smb PROPERTIES OUTPUT_NAME smb-${CMAKE_BUILD_TYPE})
Expand Down
8 changes: 8 additions & 0 deletions include/AABB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "Components.h"

bool AABBCollision(
TransformComponent* a,
TransformComponent* b
);
2 changes: 1 addition & 1 deletion include/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ constexpr float MARIO_BOUNCE = 4.0f; // when jumping on top of enemies
constexpr int TILE_ROUNDNESS = 4;

constexpr float MUSHROOM_GROW_SPEED = .25f;
constexpr float MUSHROOM_MOVE_SPEED = 1.0f;
constexpr float MUSHROOM_MOVE_SPEED = 0.8f;
1 change: 1 addition & 0 deletions include/systems/EnemySystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "ecs/ecs.h"
#include <Components.h>
#include "AABB.h"

class EnemySystem : public System {

Expand Down
1 change: 1 addition & 0 deletions include/systems/PlayerSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Components.h"
#include "Constants.h"
#include "TextureManager.h"
#include "AABB.h"

class PlayerSystem : public System {

Expand Down
12 changes: 12 additions & 0 deletions src/AABB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "AABB.h"


bool AABBCollision(
TransformComponent* a,
TransformComponent* b
) {
return a->x <= b->x + b->w &&
a->x + a->w >= b->x &&
a->y <= b->y + b->h &&
a->y + a->h >= b->y;
}
44 changes: 30 additions & 14 deletions src/systems/EnemySystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,37 @@ void EnemySystem::onAddedToWorld(World* world) {
System::onAddedToWorld(world);
}

void turtleShellInteractions(World* world, Entity* shell) {
if (shell->hasAny<LeftCollisionComponent, RightCollisionComponent>()) {
for (auto other : world->find<EnemyComponent, KineticComponent>()) {
if (shell == other) continue;
if (AABBCollision(shell->get<TransformComponent>(), other->get<TransformComponent>())) {
other->remove<SolidComponent>();
other->assign<TileComponent>();
other->get<TextureComponent>()->flipV = true;
other->get<KineticComponent>()->speedY = -8;
shell->remove<LeftCollisionComponent>();
shell->remove<RightCollisionComponent>();
}
}

if (shell->has<LeftCollisionComponent>()) {
shell->get<KineticComponent>()->accX = 3.0f;
shell->get<KineticComponent>()->speedX = 3.0f;
}
if (shell->has<RightCollisionComponent>()) {
shell->get<KineticComponent>()->accX = -3.0f;
shell->get<KineticComponent>()->speedX = -3.0f;
}
}
}

void EnemySystem::tick(World* world) {
for (auto shell : world->find<EnemyComponent, KineticComponent>()) {
if (shell->get<EnemyComponent>()->type == Enemy::Type::TURTLE_SHELL) {
turtleShellInteractions(world, shell);
}
}

for (auto enemy : world->find<EnemyComponent, TransformComponent, CrushedComponent>()) {
auto enemyTransform = enemy->get<TransformComponent>();
Expand Down Expand Up @@ -58,20 +88,6 @@ void EnemySystem::tick(World* world) {
}
}


for (auto shell : world->find<EnemyComponent, KineticComponent>()) {
if (shell->get<EnemyComponent>()->type == Enemy::Type::TURTLE_SHELL) {
if (shell->has<LeftCollisionComponent>()) {
shell->get<KineticComponent>()->accX = 3.0f;
shell->get<KineticComponent>()->speedX = 3.0f;
}
if (shell->has<RightCollisionComponent>()) {
shell->get<KineticComponent>()->accX = -3.0f;
shell->get<KineticComponent>()->speedX = -3.0f;
}
}
}

//WALKABLE
auto entities = world->find<WalkComponent, LeftCollisionComponent>();
for (auto entity : entities) {
Expand Down
20 changes: 12 additions & 8 deletions src/systems/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ Direction checkCollisionY(Entity* solid, TransformComponent* transform, KineticC
if (distanceTop < distanceBottom) {
transform->setBottom(solidTransform->top());
solid->assign<TopCollisionComponent>();
kinetic->accY = std::min(0.0f, kinetic->accY);
kinetic->speedY = std::min(0.0f, kinetic->speedY);

direction = Direction::BOTTOM;
}
}
Expand All @@ -45,8 +44,7 @@ Direction checkCollisionY(Entity* solid, TransformComponent* transform, KineticC
if (distanceTop > distanceBottom) {
transform->setTop(solidTransform->bottom());
solid->assign<BottomCollisionComponent>();
kinetic->accY = std::max(0.0f, kinetic->accY);
kinetic->speedY = std::max(0.0f, kinetic->speedY);

direction = Direction::TOP;
}
}
Expand Down Expand Up @@ -76,8 +74,7 @@ Direction checkCollisionX(Entity* solid, TransformComponent* transform, KineticC
//item about to get inside the block
transform->setLeft(solidTransform->right());
}
kinetic->accX = std::max(0.0f, kinetic->accX);
kinetic->speedX = std::max(0.0f, kinetic->speedX);

solid->assign<RightCollisionComponent>();
direction = Direction::LEFT;
} else {
Expand All @@ -87,8 +84,7 @@ Direction checkCollisionX(Entity* solid, TransformComponent* transform, KineticC
} else {
transform->setRight(solidTransform->left());
}
kinetic->accX = std::min(0.0f, kinetic->accX);
kinetic->speedX = std::min(0.0f, kinetic->speedX);

solid->assign<LeftCollisionComponent>();
direction = Direction::RIGHT;
}
Expand Down Expand Up @@ -158,9 +154,13 @@ void PhysicsSystem::tick(World* world) {
if (!(tile->get<SolidComponent>())) continue;
switch (checkCollisionY(tile, transform, kinetic)) {
case Direction::TOP:
kinetic->accY = std::max(0.0f, kinetic->accY);
kinetic->speedY = std::max(0.0f, kinetic->speedY);
entity->assign<TopCollisionComponent>();
break;
case Direction::BOTTOM:
kinetic->accY = std::min(0.0f, kinetic->accY);
kinetic->speedY = std::min(0.0f, kinetic->speedY);
entity->assign<BottomCollisionComponent>();
break;
default:
Expand All @@ -172,9 +172,13 @@ void PhysicsSystem::tick(World* world) {
if (!(tile->get<SolidComponent>())) continue;
switch (checkCollisionX(tile, transform, kinetic)) {
case Direction::LEFT:
kinetic->accX = std::max(0.0f, kinetic->accX);
kinetic->speedX = std::max(0.0f, kinetic->speedX);
entity->assign<LeftCollisionComponent>();
break;
case Direction::RIGHT:
kinetic->accX = std::min(0.0f, kinetic->accX);
kinetic->speedX = std::min(0.0f, kinetic->speedX);
entity->assign<RightCollisionComponent>();
break;
default:
Expand Down
10 changes: 0 additions & 10 deletions src/systems/PlayerSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ const int SUPER_MARIO_COLLIDER_HEIGHT = (TILE_SIZE * 2) - 4;

constexpr int RUNNING_ANIMATION_SPEED = 5;

bool AABBCollision(
TransformComponent* a,
TransformComponent* b
) {
return a->x <= b->x + b->w &&
a->x + a->w >= b->x &&
a->y <= b->y + b->h &&
a->y + a->h >= b->y;
}

void createDebris(World* world, TransformComponent* brickTransform) {
auto debris1 = world->create();
debris1->assign<GravityComponent>();
Expand Down

0 comments on commit ed3cb8b

Please sign in to comment.