Skip to content

Commit

Permalink
Add Module 3: Object-Oriented Programming and Ownership Semantics (#25)
Browse files Browse the repository at this point in the history
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
3 people authored Feb 15, 2024
1 parent 68c75b0 commit be7ef31
Show file tree
Hide file tree
Showing 447 changed files with 16,067 additions and 752 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ sfml/*

Makefile
CMakeCache.txt
.DS_Store
2 changes: 1 addition & 1 deletion MemoryManagement/LinkedList/LinkedList/src/approaching.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "scene.hpp"
#include "game.hpp"

void approachingLoop(Circle player, Circle consumable[], bool concerned[], int size) {
for (int i = 0; i < size; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion MemoryManagement/LinkedList/LinkedList/src/borders.cpp
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;
Expand Down
2 changes: 1 addition & 1 deletion MemoryManagement/LinkedList/LinkedList/src/collision.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <cmath>

#include "scene.hpp"
#include "game.hpp"

float distance(Point2D a, Point2D b) {
float dx = a.x - b.x;
Expand Down
2 changes: 1 addition & 1 deletion MemoryManagement/LinkedList/LinkedList/src/direction.cpp
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) {
Expand Down
2 changes: 1 addition & 1 deletion MemoryManagement/LinkedList/LinkedList/src/generate.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "scene.hpp"
#include "game.hpp"

#include <cstdlib>

Expand Down
2 changes: 1 addition & 1 deletion MemoryManagement/LinkedList/LinkedList/src/loop.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "scene.hpp"
#include "game.hpp"

void collisionLoop(Circle player, Circle consumable[], bool consumed[], int size) {
for (int i = 0; i < size; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion MemoryManagement/LinkedList/LinkedList/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <SFML/Graphics.hpp>

#include "scene.hpp"
#include "game.hpp"
#include "dllist.hpp"

const int MAX_CONSUMABLES_COUNT = 8;
Expand Down
2 changes: 1 addition & 1 deletion MemoryManagement/LinkedList/LinkedList/src/point.cpp
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 };
Expand Down
4 changes: 2 additions & 2 deletions MemoryManagement/LinkedList/LinkedList/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void link(Node* cursor, Node* node);
It should link together the `cursor` and `node` nodes, putting `node` right after `cursor`.
Make sure to properly update the `next` and `prev` fields of all relevant nodes,
that is, the nodes pointed by the `cursor`, `cursor->next`, and `node` pointers.
You might assume that all nodes reachable from `cursor` through `next` and `prev`
You might assume that all nodes which are reachable from `cursor` through `next` and `prev`
are valid nodes and none of their `next` or `prev` pointers are null
(we will see why this is true for our intended list implementation later).
Expand Down Expand Up @@ -91,7 +91,7 @@ struct List {
For this scheme to work properly, we also have to initialize the
`next` and `prev` fields of the `sentry` node.
We can make them both point to the `sentry` node.
Therefore, in our encoding, an empty list is modelled as
Therefore, in our encoding, an empty list is modeled as
a list consisting of a single sentinel node whose `next` and `prev` pointers form a cycle.
Write code for the function `initList` implementing this idea
(set the `data` field of the `sentry` node to `nullptr`):
Expand Down
4 changes: 2 additions & 2 deletions MemoryManagement/MemoryLayout/Swap/task-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ type: edu
files:
- name: CMakeLists.txt
visible: false
- name: test/test.cpp
visible: false
- name: src/task.cpp
visible: true
placeholders:
- offset: 32
length: 36
placeholder_text: /* TODO */
- name: test/test.cpp
visible: false
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ custom_name: C Style Strings Concatenation
files:
- name: CMakeLists.txt
visible: false
- name: test/test.cpp
visible: false
- name: src/task.cpp
visible: true
placeholders:
- offset: 92
length: 201
placeholder_text: return nullptr;
- name: test/test.cpp
visible: false
2 changes: 1 addition & 1 deletion MemoryManagement/TypeCastsAndCStrings/CStyle/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ However, in general, C is not a strict subset of C++, meaning that
there are a lot of various [incompatibilities](https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B)
between these languages.
Besides that, there are a lot of language features, idioms, and patterns that differ in C and C++.
These difference give rise to the mentioned C and C++ styles of doing things.
These differences give rise to the mentioned C and C++ styles of doing things.

It is important to learn to read C-style code, even if you plan to follow purely the C++ style in the future.
As of today, the C language has become a "cross-platform assembly language",
Expand Down
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
)
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);
}
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);
}
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;
}
}
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 };
}
}
Loading

0 comments on commit be7ef31

Please sign in to comment.