-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Module 3: Object-Oriented Programming and Ownership Semantics (#25)
add module 3: object-oriented programming and ownership semantics (#25) --------- Signed-off-by: Evgenii Moiseenko <evg.moiseenko94@gmail.com> Co-authored-by: Dmitrii Kotov <87141565+boruno@users.noreply.github.com> Co-authored-by: Mikhail Oshukov <Mikhail.Oshukov@jetbrains.com>
- Loading branch information
1 parent
68c75b0
commit be7ef31
Showing
447 changed files
with
16,067 additions
and
752 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ sfml/* | |
|
||
Makefile | ||
CMakeCache.txt | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include "scene.hpp" | ||
#include "game.hpp" | ||
|
||
Point2D adjustToBorders(Point2D position) { | ||
Point2D result = position; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include "scene.hpp" | ||
#include "game.hpp" | ||
|
||
Point2D getDirection(Direction direction) { | ||
switch (direction) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include "scene.hpp" | ||
#include "game.hpp" | ||
|
||
#include <cstdlib> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include "scene.hpp" | ||
#include "game.hpp" | ||
|
||
Point2D add(Point2D a, Point2D b) { | ||
Point2D c = { 0, 0 }; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
ObjectOrientedProgramming/ClassesAndObjects/CollisionsRevisited/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
cmake_minimum_required(VERSION 3.25) | ||
|
||
project(ObjectOrientedProgramming-ClassesAndObjects-CollisionsRevisited) | ||
|
||
set(SRC | ||
src/main.cpp | ||
src/engine.cpp src/scenes.cpp src/textures.cpp | ||
src/scene.cpp src/statscene.cpp src/dynscene.cpp | ||
src/gobject.cpp src/gobjectlist.cpp src/cgobject.cpp | ||
src/player.cpp src/consumable.cpp src/enemy.cpp | ||
src/collision.cpp src/direction.cpp | ||
src/rectangle.cpp src/point.cpp | ||
src/operators.cpp src/utils.cpp | ||
) | ||
|
||
set(TEST | ||
test/test.cpp) | ||
|
||
add_executable(${PROJECT_NAME}-run ${SRC}) | ||
|
||
configure_test_target(${PROJECT_NAME}-test "${SRC}" ${TEST}) | ||
|
||
prepare_sfml_framework_lesson_task( | ||
"${CMAKE_CURRENT_SOURCE_DIR}/.." | ||
${PROJECT_NAME}-run | ||
${PROJECT_NAME}-test | ||
) |
47 changes: 47 additions & 0 deletions
47
ObjectOrientedProgramming/ClassesAndObjects/CollisionsRevisited/src/cgobject.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "cgobject.hpp" | ||
|
||
#include "operators.hpp" | ||
|
||
CircleGameObject::CircleGameObject(Circle circle) | ||
: circle(circle) | ||
, status(GameObjectStatus::NORMAL) | ||
{} | ||
|
||
Point2D CircleGameObject::getPosition() const { | ||
return circle.center; | ||
} | ||
|
||
void CircleGameObject::setPosition(Point2D position) { | ||
circle.center = position; | ||
} | ||
|
||
GameObjectStatus CircleGameObject::getStatus() const { | ||
return status; | ||
} | ||
|
||
void CircleGameObject::setStatus(GameObjectStatus newStatus) { | ||
status = newStatus; | ||
} | ||
|
||
Circle CircleGameObject::getCircle() const { | ||
return circle; | ||
} | ||
|
||
Rectangle CircleGameObject::getBoundingBox() const { | ||
Point2D offset = { circle.radius, circle.radius }; | ||
Point2D p1 = circle.center - offset; | ||
Point2D p2 = circle.center + offset; | ||
return createRectangle(p1, p2); | ||
} | ||
|
||
void CircleGameObject::draw(sf::RenderWindow &window, TextureManager& textureManager) const { | ||
const sf::Texture* texture = getTexture(textureManager); | ||
if (texture == nullptr) | ||
return; | ||
sf::CircleShape shape; | ||
shape.setPosition(circle.center.x, circle.center.y); | ||
shape.setOrigin(circle.radius, circle.radius); | ||
shape.setRadius(circle.radius); | ||
shape.setTexture(texture); | ||
window.draw(shape); | ||
} |
21 changes: 21 additions & 0 deletions
21
ObjectOrientedProgramming/ClassesAndObjects/CollisionsRevisited/src/collision.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <cassert> | ||
|
||
#include "collision.hpp" | ||
#include "cgobject.hpp" | ||
#include "utils.hpp" | ||
|
||
CollisionInfo collisionInfo(const Circle& circle1, const Circle& circle2) { | ||
CollisionInfo info; | ||
info.distance = distance(circle1.center, circle2.center); | ||
info.collide = (info.distance < circle1.radius + circle2.radius); | ||
return info; | ||
} | ||
|
||
CollisionInfo collisionInfo(const GameObject& object1, const GameObject& object2) { | ||
const CircleGameObject* circleObject1 = dynamic_cast<const CircleGameObject*>(&object1); | ||
const CircleGameObject* circleObject2 = dynamic_cast<const CircleGameObject*>(&object2); | ||
if (circleObject1 && circleObject2) { | ||
return collisionInfo(circleObject1->getCircle(), circleObject2->getCircle()); | ||
} | ||
assert(false); | ||
} |
46 changes: 46 additions & 0 deletions
46
ObjectOrientedProgramming/ClassesAndObjects/CollisionsRevisited/src/consumable.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "consumable.hpp" | ||
|
||
#include "constants.hpp" | ||
|
||
ConsumableObject::ConsumableObject() | ||
: CircleGameObject({ { CONSUMABLE_START_X, CONSUMABLE_START_Y }, CONSUMABLE_RADIUS }) | ||
{} | ||
|
||
GameObjectKind ConsumableObject::getKind() const { | ||
return GameObjectKind::CONSUMABLE; | ||
} | ||
|
||
Point2D ConsumableObject::getVelocity() const { | ||
return { 0.0f, 0.0f }; | ||
} | ||
|
||
void ConsumableObject::update(sf::Time delta) { | ||
if (getStatus() != GameObjectStatus::DESTROYED) { | ||
setStatus(GameObjectStatus::NORMAL); | ||
} | ||
} | ||
|
||
void ConsumableObject::onCollision(const GameObject &object, const CollisionInfo &info) { | ||
if (getStatus() == GameObjectStatus::DESTROYED || object.getKind() == GameObjectKind::CONSUMABLE) { | ||
return; | ||
} | ||
if (info.collide) { | ||
setStatus(GameObjectStatus::DESTROYED); | ||
return; | ||
} | ||
if (info.distance < CONSUMABLE_WARNED_MULTIPLIER * getCircle().radius) { | ||
setStatus(GameObjectStatus::WARNED); | ||
return; | ||
} | ||
} | ||
|
||
const sf::Texture* ConsumableObject::getTexture(TextureManager& textureManager) const { | ||
switch (getStatus()) { | ||
case GameObjectStatus::NORMAL: | ||
return textureManager.getTexture(GameTextureID::STAR); | ||
case GameObjectStatus::WARNED: | ||
return textureManager.getTexture(GameTextureID::STAR_CONCERNED); | ||
case GameObjectStatus::DESTROYED: | ||
return nullptr; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
ObjectOrientedProgramming/ClassesAndObjects/CollisionsRevisited/src/direction.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "direction.hpp" | ||
|
||
Point2D getDirection(Direction direction) { | ||
switch (direction) { | ||
case North: | ||
return { 0.0f, -1.0f }; | ||
case East: | ||
return { 1.0f, 0.0f }; | ||
case South: | ||
return { 0.0f, 1.0f }; | ||
case West: | ||
return { -1.0f, 0.0f }; | ||
default: | ||
return { 0.0f, 0.0f }; | ||
} | ||
} |
Oops, something went wrong.