Skip to content

Commit

Permalink
added comment
Browse files Browse the repository at this point in the history
  • Loading branch information
denitdao committed May 29, 2019
1 parent 587df2c commit fb282f5
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 196 deletions.
91 changes: 48 additions & 43 deletions firstSFML/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,95 +3,102 @@

class Actor : public Block {
protected:
t_direcrion dir;
float yJumpSpeed = gravity_speed;
sf::Vector2f respawnPos;
bool isPlayer = false;
t_direcrion dir; // enum of entity's direction
float yJumpSpeed = GRAVITY_SPEED; // falling speed
sf::Vector2f respawnPos; // coords of the respawn
bool isPlayer = false; // entity is bot or player
public:
int xMap, yMap;
bool coinAmountIncrease = false;
int xMap, yMap; // coords of an area where entity located
bool coinAmountIncrease = false; // should we increase coin amount ?
bool inJump = false;
bool onLeftStair = false;
bool onRightStair = false;
bool upArrowPressed = false;
bool downArrowPressed = false;
bool leftArrowPressed = false;
bool rightArrowPressed = false;
Actor(sf::Vector2f size) : Block(size) { //
object.setFillColor(sf::Color::Blue);
// create shape
Actor(sf::Vector2f size) : Block(size) {
dir = none;
}
void moveOn(sf::Vector2f distance) {
// move entity on a distance
void moveOn(sf::Vector2f distance) {
distance.x *= dir;
object.move(distance);
sprite.move(distance);
}
void setRespawn(sf::Vector2i coord) {
// initialize respawnPos
void setRespawn(sf::Vector2i coord) {
respawnPos = {coord.x * BLOCK_SIZE_X, coord.y * BLOCK_SIZE_Y - BLOCK_SIZE_Y };
}
void respawn() {
// move player to the respawnPos
void respawn() {
inJump = false;
onLeftStair = false;
onRightStair = false;
upArrowPressed = false;
downArrowPressed = false;
setPos(respawnPos);
}
void setDir(enum direction d) {
// initialize direction enum
void setDir(enum direction d) {
dir = d;
}
void initJumpSpeed() {
yJumpSpeed = gravity_speed;
// set falling speed value to max
void initJumpSpeed() {
yJumpSpeed = GRAVITY_SPEED;
}
void jump() {
// jump or fall
void jump() {
myLog(Logger::DEBUG) << "inJump - " << ((inJump) ? "true" : "false") << endl;

if (inJump == true) {
lockJump = true;
moveOn({ x_move_speed, yJumpSpeed });
myLog(Logger::DEBUG) << "Jumping on { " << x_move_speed << " ; " << yJumpSpeed << " }" << endl;
yJumpSpeed += jump_change_step;
if (yJumpSpeed >= 10)
yJumpSpeed = 0;
moveOn({ X_MOVE_SPEED, yJumpSpeed }); // continue jump
myLog(Logger::DEBUG) << "Jumping on { " << X_MOVE_SPEED << " ; " << yJumpSpeed << " }" << endl;
yJumpSpeed += JUMP_CHANGE_STEP; // change Oy speed value
if (yJumpSpeed >= -GRAVITY_SPEED * 2) // limit falling speed
yJumpSpeed = -GRAVITY_SPEED * 2;
}
else {
if (!onLeftStair && !onRightStair) {
myLog(Logger::DEBUG) << "Falling down, Y " << -gravity_speed << std::endl;
moveOn({ 0, -gravity_speed });
if (!onLeftStair && !onRightStair) { // unable to jump or fall being on the stair
myLog(Logger::DEBUG) << "Falling down, Y " << -GRAVITY_SPEED << std::endl;
moveOn({ 0, -GRAVITY_SPEED }); // fall down
lockJump = true;
}
if (collision == bColl) {
if (collision == bColl) { // if we are on the ground
initJumpSpeed();
}
}
if (collision == bColl || collision == tColl || collision == rColl || collision == lColl) {
inJump = false;
inJump = false; // stop jumping if we have any contact with surface
}
}
// more acc values can be decreased
int checkCollision(Block &obj2) {
// check entitiy's collision with an object
// empty texture - 4 | coin - 3 | bot - 2 | shape - 1 | no collision - 0
int checkCollision(Block &obj2) {
t_texture objSkin = obj2.getTexture();
if (objSkin == noTexture)
if (objSkin == noTexture) // do not collide with empty blocks
return 4;
float deltaX = this->getSenter().x - obj2.getSenter().x;
float deltaY = this->getSenter().y - obj2.getSenter().y;
float intersectX = abs(deltaX) - (this->getSize().x / 2 + obj2.getSize().x / 2); // < 0
float intersectY = abs(deltaY) - (this->getSize().y / 2 + obj2.getSize().y / 2); // < 0

if (objSkin != stairLeft && objSkin != stairRight) {
if (objSkin != stairLeft && objSkin != stairRight) {
if (intersectX < 0.0f && intersectY < 0.0f) {
if (objSkin == bot)
if (objSkin == bot) // colliding with a mummy
return 2;
if (objSkin == coin) {
if (isPlayer) {
if (isPlayer) { // player collides with a coin
obj2.hideCoin();
coinAmountIncrease = true;
return 3;
}
return 0;
}
myLog(Logger::DEBUG) << "Colliding | intersectX = " << intersectX << "| intersectY = " << intersectY << endl;
if (intersectY < intersectX) {
if (intersectY < intersectX) { // move in the direction of higher intersect
if (deltaX < 0) { // right intersect
collision = rColl;
myLog(Logger::DEBUG) << "right intersect, move X on " << intersectX << endl;
Expand All @@ -109,15 +116,13 @@ class Actor : public Block {
else {
if (deltaY < 0) { // bottom intersect
collision = bColl;
if (objSkin == bot)
respawn();
lockJump = false;
myLog(Logger::DEBUG) << "bottom intersect, move Y on " << intersectY << std::endl;
// Now check if we are near/on the stair
switch (objSkin) {
case stairLeftUnder: {
myLog(Logger::DEBUG) << "\n\nChecking stairLeftUnder\n\n" << endl;
if (obj2.getSenter().x - x_move_speed <= this->getCoord().x && obj2.getSenter().x >= this->getCoord().x) { // senter of stair block = left side + 1 of the player
if (obj2.getSenter().x - X_MOVE_SPEED <= this->getCoord().x && obj2.getSenter().x >= this->getCoord().x) { // senter of stair block = left side + 1 of the player
myLog(Logger::DEBUG) << "\n\nSenter\n\n" << endl;
if (onLeftStair) {
if (this->dir == toright) {
Expand All @@ -129,7 +134,7 @@ class Actor : public Block {
else { // not on stair
if (this->dir == toleft && this->upArrowPressed) {
onLeftStair = true;
setPos({ obj2.getSenter().x - x_move_speed, this->getCoord().y }); // for more accurate process
setPos({ obj2.getSenter().x - X_MOVE_SPEED, this->getCoord().y }); // for more accurate process
myLog(Logger::DEBUG) << "\n\nPlayer is now onLeftStair = T R U E\n\n" << endl;
break;
}
Expand All @@ -142,7 +147,7 @@ class Actor : public Block {
}
case stairLeftTop: {
myLog(Logger::DEBUG) << "\n\nChecking stairLeftTop\n\n" << endl;
if (obj2.getSenter().x - x_move_speed <= this->getCoord().x && obj2.getSenter().x >= this->getCoord().x) { // senter of stair block = left side of the player
if (obj2.getSenter().x - X_MOVE_SPEED <= this->getCoord().x && obj2.getSenter().x >= this->getCoord().x) { // senter of stair block = left side of the player
myLog(Logger::DEBUG) << "\n\nSenter\n\n" << endl;
if (onLeftStair) {
if (this->dir == toleft) {
Expand All @@ -153,7 +158,7 @@ class Actor : public Block {
else { // not on stair
if (this->dir == toright && this->downArrowPressed) {
onLeftStair = true;
setPos({ obj2.getSenter().x - x_move_speed, this->getCoord().y }); // for more accurate process
setPos({ obj2.getSenter().x - X_MOVE_SPEED, this->getCoord().y }); // for more accurate process
myLog(Logger::DEBUG) << "\n\nPlayer is now onLeftStair = T R U E\n\n" << endl;
break;
}
Expand All @@ -166,7 +171,7 @@ class Actor : public Block {
}
case stairRightUnder: {
myLog(Logger::DEBUG) << "\n\nChecking stairRightUnder\n\n" << endl;
if (obj2.getSenter().x <= this->getCoord().x + this->getSize().x && obj2.getSenter().x + x_move_speed >= this->getCoord().x + this->getSize().x) { // senter of stair block = right side of the player
if (obj2.getSenter().x <= this->getCoord().x + this->getSize().x && obj2.getSenter().x + X_MOVE_SPEED >= this->getCoord().x + this->getSize().x) { // senter of stair block = right side of the player
myLog(Logger::DEBUG) << "\n\nSenter\n\n" << endl;
if (onRightStair) {
if (this->dir == toleft) {
Expand All @@ -177,7 +182,7 @@ class Actor : public Block {
else { // not on stair
if (this->dir == toright && this->upArrowPressed) {
onRightStair = true;
setPos({ obj2.getSenter().x - this->getSize().x + x_move_speed, this->getCoord().y }); // for more accurate process
setPos({ obj2.getSenter().x - this->getSize().x + X_MOVE_SPEED, this->getCoord().y }); // for more accurate process
myLog(Logger::DEBUG) << "\n\nPlayer is now onRightStair = T R U E\n\n" << endl; ///!!!!!!!!!!!!!!!!!!!
break;
}
Expand All @@ -190,7 +195,7 @@ class Actor : public Block {
}
case stairRightTop: {
myLog(Logger::DEBUG) << "\n\nChecking stairRightTop\n\n" << endl;
if (obj2.getSenter().x <= this->getCoord().x + this->getSize().x && obj2.getSenter().x + x_move_speed >= this->getCoord().x + this->getSize().x) { // senter of stair block = right side of the player
if (obj2.getSenter().x <= this->getCoord().x + this->getSize().x && obj2.getSenter().x + X_MOVE_SPEED >= this->getCoord().x + this->getSize().x) { // senter of stair block = right side of the player
myLog(Logger::DEBUG) << "\n\nSenter\n\n" << endl;
if (onRightStair) {
if (this->dir == toright) {
Expand All @@ -201,7 +206,7 @@ class Actor : public Block {
else { // not on stair
if (this->dir == toleft && this->downArrowPressed) {
onRightStair = true;
setPos({ obj2.getSenter().x - this->getSize().x - x_move_speed, this->getCoord().y }); // for more accurate process
setPos({ obj2.getSenter().x - this->getSize().x - X_MOVE_SPEED, this->getCoord().y }); // for more accurate process
myLog(Logger::DEBUG) << "\n\nPlayer is now onRightStair = T R U E\n\n" << endl;
break;
}
Expand All @@ -224,7 +229,7 @@ class Actor : public Block {
myLog(Logger::DEBUG) << "Checking default" << endl;
if (!onLeftStair && !onRightStair)
setPos({ this->getCoord().x , obj2.getSenter().y - (this->getSize().y + obj2.getSize().y / 2) });
else if (intersectY > -x_move_speed) { // inters Y small
else if (intersectY > -X_MOVE_SPEED) { // inters Y small
onLeftStair = false;
onRightStair = false;
}
Expand Down
46 changes: 29 additions & 17 deletions firstSFML/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,57 @@ using namespace std;
class Block {
protected:
sf::RectangleShape object;
sf::Texture texture;
sf::Texture texture; // pointer to a picture
sf::Sprite sprite;
t_collided collision;
t_texture skin = wall;
bool coinCollected = false;
public:
bool lockJump = true;

Block() {}
Block(sf::Vector2f size) { //
object.setSize(size); //
// create instance of the class without initializing
Block() {}
// initialize shape
Block(sf::Vector2f size) {
object.setSize(size);
object.setFillColor(sf::Color::Green);
}
Block(sf::Vector2f size, string fname) { //
object.setSize(size); //
// initialize shape and set texture
Block(sf::Vector2f size, string fname) {
object.setSize(size);
object.setFillColor(sf::Color::Green);
// making sprite with the same parameters
if (!texture.loadFromFile(fname)) {
myLog(Logger::ERR) << "image load failed!" << endl;
}
sprite.setTextureRect(sf::IntRect(0, 0, texture.getSize().x, texture.getSize().y));
sprite.setTextureRect(sf::IntRect(0, 0, texture.getSize().x, texture.getSize().y)); // load full picture
sprite.setTexture(texture);
sprite.setScale({size.x / texture.getSize().x , size.y / texture.getSize().y });
}
//noTexture = 0, wall = 1, stairLeftUnder = 2, stairLeft = 3, stairLeftTop = 4, stairRightUnder = 5, stairRight = 6, stairRightTop = 7, hardWall = 8
void annulateCollision() { // at the beginning of the lap, no collision
// at the beginning of the lap, no collision
void annulateCollision() {
collision = no;
}
sf::Vector2f getSenter() { // return center of shape
// return the coords of the senter of shape
sf::Vector2f getSenter() {
return { object.getPosition().x + object.getSize().x / 2 , object.getPosition().y + object.getSize().y / 2 };
}
sf::Vector2f getCoord() {
return object.getPosition();
// return the coords of the top left corner of shape
sf::Vector2f getCoord() {
return object.getPosition();
}
void drawTo(sf::RenderWindow &window) {
// draw shape or texture to the window
void drawTo(sf::RenderWindow &window) {
if (skin != noTexture) {
if(xRay)
window.draw(object);
else
window.draw(sprite);
}
}
void setPos(sf::Vector2f newPosition) {
// move the top left corner of shape
void setPos(sf::Vector2f newPosition) {
object.setPosition(newPosition);
if(skin == stairLeft || skin == stairLeftTop)
sprite.setPosition({ newPosition.x + PLAYER_SIZE_X / 3, newPosition.y }); // not proper begin moving on stairs
Expand All @@ -59,13 +67,16 @@ class Block {
else
sprite.setPosition(newPosition);
}
sf::Vector2f getSize() {
// get height and width
sf::Vector2f getSize() {
return object.getSize();
}
t_texture getTexture() {
// get enum of the current texture
t_texture getTexture() {
return skin;
}
void create(t_texture nskin, int current_level) { //
// initialize texture enum, shape, coose and set texture
void create(t_texture nskin, int current_level) {
skin = nskin;
switch (skin) {
case noTexture: {
Expand Down Expand Up @@ -204,7 +215,8 @@ class Block {
}
}
}
void hideCoin() {
// set empty texture to coin
void hideCoin() {
if (!texture.loadFromFile("images/empty.png")) {
myLog(Logger::ERR) << "image load failed!" << endl;
}
Expand Down
34 changes: 14 additions & 20 deletions firstSFML/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,37 @@

// screen settings
constexpr float ASPECT_RATIO = 3.f / 4.f;
constexpr float WINDOW_SIZE_X = 1024.f;
constexpr float WINDOW_SIZE_X = 1024.f; // 1024.f
constexpr float WINDOW_SIZE_Y = WINDOW_SIZE_X * ASPECT_RATIO;
constexpr int FPS_LOCK = 60;

// map settings
constexpr int MAX_MAP_SIZE_X = 64;
constexpr int MAP_SIZE_X = 32;
constexpr int MAP_SIZE_Y = 24;
constexpr float BLOCK_SIZE_X = WINDOW_SIZE_X / MAP_SIZE_X;
constexpr float BLOCK_SIZE_Y = WINDOW_SIZE_Y / MAP_SIZE_Y;

static bool xRay = false;
constexpr int LIVES_CONST = 4;
static int lives = LIVES_CONST;

// player settings
constexpr float PLAYER_SIZE_X = WINDOW_SIZE_X / MAP_SIZE_X;
constexpr float PLAYER_SIZE_Y = (WINDOW_SIZE_Y / MAP_SIZE_Y) * 2;
constexpr int LIVES_CONST = 4;
constexpr float CHANGE_TEXTURE_DELTA_X = 10.f;

static int lives = LIVES_CONST;

// gravity settings
static float gravity_speed = 0.f;
static float x_move_speed = 0.f;
static float jump_change_step = 0.f;
static float stair_move_speed = 0.f;
//Denys
constexpr float GRAVITY_SPEED_CONST = -5.0f;
constexpr float X_MOVE_SPEED_CONST = 2.0f;
constexpr float JUMP_CHANGE_STEP_CONST = 0.15f;
//Alex
constexpr float GRAVITY_SPEED_CONST1 = -4.7f;
constexpr float X_MOVE_SPEED_CONST1 = 4.0f;
constexpr float JUMP_CHANGE_STEP_CONST1 = 0.12f;
constexpr float X_MOVE_SPEED = WINDOW_SIZE_X / 512.f; // 2.0f
constexpr float GRAVITY_SPEED = X_MOVE_SPEED * -2.5f; // -5.0f
constexpr float JUMP_CHANGE_STEP = GRAVITY_SPEED * (-3.f / 100.f); // 0.15f

// enums
typedef enum direction { toleft = -1, none = 0, toright = 1 } t_direcrion;
typedef enum condition { normal, inJump, onStairs } t_condition;
typedef enum collided { rColl, lColl, bColl, tColl, no, colliding } t_collided;
typedef enum heroMovement {} t_heroMovement;
typedef enum mummyMovement {} t_mummyMovement;
typedef enum texture { noTexture = 0, wall = 1, stairLeftUnder = 2, stairLeft = 3, stairLeftTop = 4, stairRightUnder = 5, stairRight = 6, stairRightTop = 7, hardWall = 8, bot = 9, coin = '*' } t_texture;
typedef enum textureDir { leftTexture = 0, rightTexture = 1} t_textureDir;
typedef enum texture { noTexture = 0, wall = 1, stairLeftUnder = 2, stairLeft = 3, stairLeftTop = 4, stairRightUnder = 5, stairRight = 6, stairRightTop = 7, hardWall = 8, bot = 9, coin = '*' } t_texture;
typedef enum collided { rColl, lColl, bColl, tColl, no, colliding } t_collided;

// logging
static Logger myLog(Logger::ERR, Logger::WARN);
Expand Down
Loading

0 comments on commit fb282f5

Please sign in to comment.