Skip to content

Commit be7ef31

Browse files
euppborunostephen-hero
authored
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>
1 parent 68c75b0 commit be7ef31

File tree

447 files changed

+16067
-752
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

447 files changed

+16067
-752
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ sfml/*
99

1010
Makefile
1111
CMakeCache.txt
12+
.DS_Store

MemoryManagement/LinkedList/LinkedList/src/approaching.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "scene.hpp"
1+
#include "game.hpp"
22

33
void approachingLoop(Circle player, Circle consumable[], bool concerned[], int size) {
44
for (int i = 0; i < size; ++i) {

MemoryManagement/LinkedList/LinkedList/src/borders.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "scene.hpp"
1+
#include "game.hpp"
22

33
Point2D adjustToBorders(Point2D position) {
44
Point2D result = position;

MemoryManagement/LinkedList/LinkedList/src/collision.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <cmath>
22

3-
#include "scene.hpp"
3+
#include "game.hpp"
44

55
float distance(Point2D a, Point2D b) {
66
float dx = a.x - b.x;

MemoryManagement/LinkedList/LinkedList/src/direction.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "scene.hpp"
1+
#include "game.hpp"
22

33
Point2D getDirection(Direction direction) {
44
switch (direction) {

MemoryManagement/LinkedList/LinkedList/src/generate.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "scene.hpp"
1+
#include "game.hpp"
22

33
#include <cstdlib>
44

MemoryManagement/LinkedList/LinkedList/src/loop.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "scene.hpp"
1+
#include "game.hpp"
22

33
void collisionLoop(Circle player, Circle consumable[], bool consumed[], int size) {
44
for (int i = 0; i < size; ++i) {

MemoryManagement/LinkedList/LinkedList/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <SFML/Graphics.hpp>
22

3-
#include "scene.hpp"
3+
#include "game.hpp"
44
#include "dllist.hpp"
55

66
const int MAX_CONSUMABLES_COUNT = 8;

MemoryManagement/LinkedList/LinkedList/src/point.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "scene.hpp"
1+
#include "game.hpp"
22

33
Point2D add(Point2D a, Point2D b) {
44
Point2D c = { 0, 0 };

MemoryManagement/LinkedList/LinkedList/task.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void link(Node* cursor, Node* node);
5555
It should link together the `cursor` and `node` nodes, putting `node` right after `cursor`.
5656
Make sure to properly update the `next` and `prev` fields of all relevant nodes,
5757
that is, the nodes pointed by the `cursor`, `cursor->next`, and `node` pointers.
58-
You might assume that all nodes reachable from `cursor` through `next` and `prev`
58+
You might assume that all nodes which are reachable from `cursor` through `next` and `prev`
5959
are valid nodes and none of their `next` or `prev` pointers are null
6060
(we will see why this is true for our intended list implementation later).
6161
@@ -91,7 +91,7 @@ struct List {
9191
For this scheme to work properly, we also have to initialize the
9292
`next` and `prev` fields of the `sentry` node.
9393
We can make them both point to the `sentry` node.
94-
Therefore, in our encoding, an empty list is modelled as
94+
Therefore, in our encoding, an empty list is modeled as
9595
a list consisting of a single sentinel node whose `next` and `prev` pointers form a cycle.
9696
Write code for the function `initList` implementing this idea
9797
(set the `data` field of the `sentry` node to `nullptr`):

MemoryManagement/MemoryLayout/Swap/task-info.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ type: edu
22
files:
33
- name: CMakeLists.txt
44
visible: false
5+
- name: test/test.cpp
6+
visible: false
57
- name: src/task.cpp
68
visible: true
79
placeholders:
810
- offset: 32
911
length: 36
1012
placeholder_text: /* TODO */
11-
- name: test/test.cpp
12-
visible: false

MemoryManagement/TypeCastsAndCStrings/CStringConcat/task-info.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ custom_name: C Style Strings Concatenation
33
files:
44
- name: CMakeLists.txt
55
visible: false
6-
- name: test/test.cpp
7-
visible: false
86
- name: src/task.cpp
97
visible: true
108
placeholders:
119
- offset: 92
1210
length: 201
1311
placeholder_text: return nullptr;
12+
- name: test/test.cpp
13+
visible: false

MemoryManagement/TypeCastsAndCStrings/CStyle/task.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ However, in general, C is not a strict subset of C++, meaning that
88
there are a lot of various [incompatibilities](https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B)
99
between these languages.
1010
Besides that, there are a lot of language features, idioms, and patterns that differ in C and C++.
11-
These difference give rise to the mentioned C and C++ styles of doing things.
11+
These differences give rise to the mentioned C and C++ styles of doing things.
1212

1313
It is important to learn to read C-style code, even if you plan to follow purely the C++ style in the future.
1414
As of today, the C language has become a "cross-platform assembly language",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
3+
project(ObjectOrientedProgramming-ClassesAndObjects-CollisionsRevisited)
4+
5+
set(SRC
6+
src/main.cpp
7+
src/engine.cpp src/scenes.cpp src/textures.cpp
8+
src/scene.cpp src/statscene.cpp src/dynscene.cpp
9+
src/gobject.cpp src/gobjectlist.cpp src/cgobject.cpp
10+
src/player.cpp src/consumable.cpp src/enemy.cpp
11+
src/collision.cpp src/direction.cpp
12+
src/rectangle.cpp src/point.cpp
13+
src/operators.cpp src/utils.cpp
14+
)
15+
16+
set(TEST
17+
test/test.cpp)
18+
19+
add_executable(${PROJECT_NAME}-run ${SRC})
20+
21+
configure_test_target(${PROJECT_NAME}-test "${SRC}" ${TEST})
22+
23+
prepare_sfml_framework_lesson_task(
24+
"${CMAKE_CURRENT_SOURCE_DIR}/.."
25+
${PROJECT_NAME}-run
26+
${PROJECT_NAME}-test
27+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "cgobject.hpp"
2+
3+
#include "operators.hpp"
4+
5+
CircleGameObject::CircleGameObject(Circle circle)
6+
: circle(circle)
7+
, status(GameObjectStatus::NORMAL)
8+
{}
9+
10+
Point2D CircleGameObject::getPosition() const {
11+
return circle.center;
12+
}
13+
14+
void CircleGameObject::setPosition(Point2D position) {
15+
circle.center = position;
16+
}
17+
18+
GameObjectStatus CircleGameObject::getStatus() const {
19+
return status;
20+
}
21+
22+
void CircleGameObject::setStatus(GameObjectStatus newStatus) {
23+
status = newStatus;
24+
}
25+
26+
Circle CircleGameObject::getCircle() const {
27+
return circle;
28+
}
29+
30+
Rectangle CircleGameObject::getBoundingBox() const {
31+
Point2D offset = { circle.radius, circle.radius };
32+
Point2D p1 = circle.center - offset;
33+
Point2D p2 = circle.center + offset;
34+
return createRectangle(p1, p2);
35+
}
36+
37+
void CircleGameObject::draw(sf::RenderWindow &window, TextureManager& textureManager) const {
38+
const sf::Texture* texture = getTexture(textureManager);
39+
if (texture == nullptr)
40+
return;
41+
sf::CircleShape shape;
42+
shape.setPosition(circle.center.x, circle.center.y);
43+
shape.setOrigin(circle.radius, circle.radius);
44+
shape.setRadius(circle.radius);
45+
shape.setTexture(texture);
46+
window.draw(shape);
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <cassert>
2+
3+
#include "collision.hpp"
4+
#include "cgobject.hpp"
5+
#include "utils.hpp"
6+
7+
CollisionInfo collisionInfo(const Circle& circle1, const Circle& circle2) {
8+
CollisionInfo info;
9+
info.distance = distance(circle1.center, circle2.center);
10+
info.collide = (info.distance < circle1.radius + circle2.radius);
11+
return info;
12+
}
13+
14+
CollisionInfo collisionInfo(const GameObject& object1, const GameObject& object2) {
15+
const CircleGameObject* circleObject1 = dynamic_cast<const CircleGameObject*>(&object1);
16+
const CircleGameObject* circleObject2 = dynamic_cast<const CircleGameObject*>(&object2);
17+
if (circleObject1 && circleObject2) {
18+
return collisionInfo(circleObject1->getCircle(), circleObject2->getCircle());
19+
}
20+
assert(false);
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "consumable.hpp"
2+
3+
#include "constants.hpp"
4+
5+
ConsumableObject::ConsumableObject()
6+
: CircleGameObject({ { CONSUMABLE_START_X, CONSUMABLE_START_Y }, CONSUMABLE_RADIUS })
7+
{}
8+
9+
GameObjectKind ConsumableObject::getKind() const {
10+
return GameObjectKind::CONSUMABLE;
11+
}
12+
13+
Point2D ConsumableObject::getVelocity() const {
14+
return { 0.0f, 0.0f };
15+
}
16+
17+
void ConsumableObject::update(sf::Time delta) {
18+
if (getStatus() != GameObjectStatus::DESTROYED) {
19+
setStatus(GameObjectStatus::NORMAL);
20+
}
21+
}
22+
23+
void ConsumableObject::onCollision(const GameObject &object, const CollisionInfo &info) {
24+
if (getStatus() == GameObjectStatus::DESTROYED || object.getKind() == GameObjectKind::CONSUMABLE) {
25+
return;
26+
}
27+
if (info.collide) {
28+
setStatus(GameObjectStatus::DESTROYED);
29+
return;
30+
}
31+
if (info.distance < CONSUMABLE_WARNED_MULTIPLIER * getCircle().radius) {
32+
setStatus(GameObjectStatus::WARNED);
33+
return;
34+
}
35+
}
36+
37+
const sf::Texture* ConsumableObject::getTexture(TextureManager& textureManager) const {
38+
switch (getStatus()) {
39+
case GameObjectStatus::NORMAL:
40+
return textureManager.getTexture(GameTextureID::STAR);
41+
case GameObjectStatus::WARNED:
42+
return textureManager.getTexture(GameTextureID::STAR_CONCERNED);
43+
case GameObjectStatus::DESTROYED:
44+
return nullptr;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "direction.hpp"
2+
3+
Point2D getDirection(Direction direction) {
4+
switch (direction) {
5+
case North:
6+
return { 0.0f, -1.0f };
7+
case East:
8+
return { 1.0f, 0.0f };
9+
case South:
10+
return { 0.0f, 1.0f };
11+
case West:
12+
return { -1.0f, 0.0f };
13+
default:
14+
return { 0.0f, 0.0f };
15+
}
16+
}

0 commit comments

Comments
 (0)