Skip to content

Commit

Permalink
Some changes 26.04
Browse files Browse the repository at this point in the history
  • Loading branch information
denitdao committed Apr 26, 2019
1 parent 12f2cd3 commit 3a2a7f5
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 27 deletions.
6 changes: 6 additions & 0 deletions SFMLprog.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "firstSFML", "firstSFML\firs
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Win32 = Release|Win32
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Debug|Win32.ActiveCfg = Debug|Win32
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Debug|Win32.Build.0 = Debug|Win32
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Debug|x64.ActiveCfg = Debug|x64
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Debug|x64.Build.0 = Debug|x64
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Debug|x86.ActiveCfg = Debug|Win32
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Debug|x86.Build.0 = Debug|Win32
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Release|Win32.ActiveCfg = Release|Win32
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Release|Win32.Build.0 = Release|Win32
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Release|x64.ActiveCfg = Release|x64
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Release|x64.Build.0 = Release|x64
{384EBE2C-FF91-4B78-A922-CB4FA666EF49}.Release|x86.ActiveCfg = Release|Win32
Expand Down
81 changes: 81 additions & 0 deletions firstSFML/Actor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once
#include "pch.h"
// ôàéë, õðàíÿùèé îïèñàíèå êëàññà "Ïåðñîíàæ"

class Actor : public Block {
protected:
t_direcrion dir;
float yJumpSpeed = gravity_speed;
const int groundHeight = 600; // êîñòûëü
public:
t_condition cond;
bool inJump = false;
Actor(sf::Vector2f size) : Block(size) { // êîíñòðóêòîð, ïîëó÷àåì ðàçìåð
object.setFillColor(sf::Color::Blue);
dir = none;
}
void moveOn(sf::Vector2f distance) {
distance.x *= dir;
object.move(distance);
}
void setDir(enum direction d) {
dir = d;
}
void initJumpSpeed() {
yJumpSpeed = gravity_speed;
}
void jump() {
std::cout << "inJump - " <<((inJump) ? "true" : "false") << std::endl;
if (inJump == true) {
moveOn({x_move_speed, yJumpSpeed});
std::cout << "Jumping on { " << x_move_speed << " ; " << yJumpSpeed <<" }" << std::endl;
yJumpSpeed += jump_change_step;
}
else {
if (collision != colliding)
std::cout << "Falling down, Y " << -gravity_speed << std::endl;
moveOn({0, -gravity_speed });
}
if (collision == colliding) {
initJumpSpeed();
inJump = false;
}
}
void checkCollision(Block &obj2) {
this->senterCalc();
obj2.senterCalc();
float deltaX = this->senterCoord.x - obj2.senterCoord.x;
float deltaY = this->senterCoord.y - obj2.senterCoord.y;
float intersectX = abs(deltaX) - (this->getSizeX() / 2 + obj2.getSizeX() / 2); // < 0
float intersectY = abs(deltaY) - (this->getSizeY() / 2 + obj2.getSizeY() / 2); // < 0

if (intersectX < 0.0f && intersectY < 0.0f) {
collision = colliding;
std::cout << "Colliding | intersectX = " << intersectX << "| intersectY = " << intersectY << std::endl;
if (intersectY < intersectX) {
if (deltaX < 0) { // right intersect
std::cout << "right intersect, move X on " << intersectX << std::endl;
this->moveOn({ intersectX, 0 });
}
if (deltaX > 0) { // left intersect
std::cout << "left intersect, move X on " << -intersectX << std::endl;
this->moveOn({ -intersectX, 0 });
}
}
else {
if (deltaY < 0) {
std::cout << "bottom intersect, move Y on " << intersectY << std::endl; // bottom intersect
this->moveOn({ 0, intersectY });
}
if (deltaY > 0) { // top intersect
std::cout << "top intersect, move Y on " << -intersectY << std::endl;
this->moveOn({ 0, -intersectY });
}
}
return;
}

std::cout << "Not colliding" << std::endl;
return;
}
};
184 changes: 184 additions & 0 deletions firstSFML/Block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#pragma once
#include "pch.h"
#include <fstream>
#include <iostream>

using namespace std;

class Block {
protected:
sf::RectangleShape object;
t_collided collision;
t_texture skin = wall;
public:
sf::Vector2f senterCoord;
Block() {
//object.setSize({ BLOCK_SIZE_X, BLOCK_SIZE_Y });
}
Block(sf::Vector2f size) { // êîíñòðóêòîð, ïîëó÷àåì ðàçìåð
object.setSize(size); // ïåðåäàåì ïîëó÷åííûå ðàçìåðû
object.setFillColor(sf::Color::Green);
}
//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
collision = no;
}
void create(t_texture nskin) { // àëüòåðíàòèâíûé êîíñòðóêòîð, ïîëó÷àåì ðàçìåð
skin = nskin;
switch (skin) {
case noTexture: {
object.setSize({ BLOCK_SIZE_X, BLOCK_SIZE_Y });
object.setFillColor(sf::Color::White);
break;
}
case wall: {
object.setSize({ BLOCK_SIZE_X, BLOCK_SIZE_Y });
object.setFillColor(sf::Color::Yellow);
break;
}
case stairLeftUnder: {
object.setSize({ BLOCK_SIZE_X, BLOCK_SIZE_Y });
object.setFillColor(sf::Color::Blue);
break;
}
case stairLeft: {
object.setSize({ 2 * BLOCK_SIZE_X, BLOCK_SIZE_Y });
object.setFillColor(sf::Color::Blue);
break;
}
case stairLeftTop: {
object.setSize({ 2 * BLOCK_SIZE_X, BLOCK_SIZE_Y });
object.setFillColor(sf::Color::Blue);
break;
}
case stairRightUnder: {
object.setSize({ BLOCK_SIZE_X, BLOCK_SIZE_Y });
object.setFillColor(sf::Color::Cyan);
break;
}
case stairRight: {
object.setSize({ 2 * BLOCK_SIZE_X, BLOCK_SIZE_Y });
object.setFillColor(sf::Color::Cyan);
break;
}
case stairRightTop: {
object.setSize({ 2 * BLOCK_SIZE_X, BLOCK_SIZE_Y });
object.setFillColor(sf::Color::Cyan);
break;
}
case hardWall: {
object.setSize({ BLOCK_SIZE_X, BLOCK_SIZE_Y });
object.setFillColor(sf::Color::Magenta);
break;
}
}
}
sf::Vector2f senterCalc() { // return center of shape
senterCoord.x = object.getPosition().x + object.getSize().x / 2;
senterCoord.y = object.getPosition().y + object.getSize().y / 2;
return { senterCoord.x , senterCoord.y };
}
void drawTo(sf::RenderWindow &window) {
if (skin != noTexture)
window.draw(object);
}
void setPos(sf::Vector2f newPosition) {
object.setPosition(newPosition);
}
float getSizeX() {
return object.getSize().x;
}
float getSizeY() {
return object.getSize().y;
}
sf::Vector2f getSize() {
return object.getSize();
}
t_texture getTexture() {
return skin;
}
void drawTo(sf::RenderWindow &window, Block newMap[MAP_SIZE_X][MAP_SIZE_Y]) {
for (int j = 0; j < MAP_SIZE_Y; j++) { // all except stair parts
for (int i = 0; i < MAP_SIZE_X; i++) {
if(newMap[i][j].getTexture() != stairLeftTop
|| newMap[i][j].getTexture() != stairRightTop
|| newMap[i][j].getTexture() != stairLeftUnder
|| newMap[i][j].getTexture() != stairRightUnder)
newMap[i][j].drawTo(window);
}
}
for (int j = 0; j < MAP_SIZE_Y; j++) {
for (int i = 0; i < MAP_SIZE_X; i++) { // only stair parts
if (newMap[i][j].getTexture() == stairLeftTop
|| newMap[i][j].getTexture() == stairRightTop
|| newMap[i][j].getTexture() == stairLeftUnder
|| newMap[i][j].getTexture() == stairRightUnder)
newMap[i][j].drawTo(window);
}
}
}
void openMap(Block newMap[MAP_SIZE_X][MAP_SIZE_Y], const char level[19]) {
ifstream mapPtr(level);
if (!mapPtr) {
cout << "error opening file - not exists" << endl;
exit(1);
}
char ch;
for (int j = 0; j < 24; j++) {
for (int i = 0; i < 32; i++) {
mapPtr.get(ch);
switch (ch) {
case '0': {
newMap[i][j].setPos({ i * BLOCK_SIZE_X, j * BLOCK_SIZE_Y });
newMap[i][j].create(noTexture);
break;
}
case '1': {
newMap[i][j].setPos({ i * BLOCK_SIZE_X, j * BLOCK_SIZE_Y });
newMap[i][j].create(wall);
break;
}
case '2': {
newMap[i][j].setPos({ (i * BLOCK_SIZE_X), j * BLOCK_SIZE_Y });
newMap[i][j].create(stairLeftUnder);
break;
}
case '3': {
newMap[i][j].setPos({ (i * BLOCK_SIZE_X) - (BLOCK_SIZE_X / 2), j * BLOCK_SIZE_Y });
newMap[i][j].create(stairLeft);
break;
}
case '4': {
newMap[i][j].setPos({ (i * BLOCK_SIZE_X) - (BLOCK_SIZE_X / 2), j * BLOCK_SIZE_Y });
newMap[i][j].create(stairLeftTop);
break;
}
case '5': {
newMap[i][j].setPos({ (i * BLOCK_SIZE_X), j * BLOCK_SIZE_Y });
newMap[i][j].create(stairRightUnder);
break;
}
case '6': {
newMap[i][j].setPos({ (i * BLOCK_SIZE_X) - (BLOCK_SIZE_X / 2), j * BLOCK_SIZE_Y });
newMap[i][j].create(stairRight);
break;
}
case '7': {
newMap[i][j].setPos({ (i * BLOCK_SIZE_X) - (BLOCK_SIZE_X / 2), j * BLOCK_SIZE_Y });
newMap[i][j].create(stairRightTop);
break;
}
case '8': {
newMap[i][j].setPos({ i * BLOCK_SIZE_X, j * BLOCK_SIZE_Y });
newMap[i][j].create(hardWall);
break;
}
}
cout << ch;
}
mapPtr.get(ch); // to read \n symbol
cout << endl;
}
mapPtr.close();
}
};
10 changes: 10 additions & 0 deletions firstSFML/Collider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include "pch.h"

class Collider {

public:
t_collided checkCollision(Block &obj1, Block &obj2) {

}
};
35 changes: 35 additions & 0 deletions firstSFML/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include "pch.h"

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

// map settings
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;

// 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;

// gravity settings
static float gravity_speed = -0.45f;
static float x_move_speed = 0.2f;
static float jump_change_step = 0.001f;

constexpr float GRAVITY_SPEED_CONST = -0.45f;
constexpr float X_MOVE_SPEED_CONST = 0.2f;
constexpr float JUMP_CHANGE_STEP_CONST = 0.001f;

// enums
typedef enum direction { toleft = -1, none = 0, toright = 1 } t_direcrion;
typedef enum condition {normal, inJump, onStairs} t_condition;
typedef enum collided { xcoll, ycoll, 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 } t_texture;

33 changes: 33 additions & 0 deletions firstSFML/Hero.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include "pch.h"

class Hero : public Actor {
enum heroMovement move;
public:
Hero(sf::Vector2f size) : Actor(size) { // êîíñòðóêòîð, ïîëó÷àåì ðàçìåð
object.setFillColor(sf::Color::Yellow);
}
void controle() {
if (inJump == false) {
setDir(none);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { // go right
setDir(toright);
moveOn({ x_move_speed, 0 });
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { // go left
setDir(toleft);
moveOn({ x_move_speed, 0 });
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { // jump
moveOn({ 0, -x_move_speed * 10.f });
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { // jump
inJump = true;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::H)) {
setPos({ 500, 536 });
}
jump();
}
};
11 changes: 11 additions & 0 deletions firstSFML/Mummy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include "pch.h"

class Mummy : public Actor {
enum mummyMovement move;
public:
Mummy(sf::Vector2f size) : Actor(size) { // ęîíńňđóęňîđ, ďîëó÷ŕĺě đŕçěĺđ
object.setFillColor(sf::Color::White);
}

};
Loading

0 comments on commit 3a2a7f5

Please sign in to comment.