From 3a2a7f55c5157ed5f6f186ae8a949da129b9a4fc Mon Sep 17 00:00:00 2001 From: denys Date: Fri, 26 Apr 2019 15:59:24 +0300 Subject: [PATCH] Some changes 26.04 --- SFMLprog.sln | 6 + firstSFML/Actor.h | 81 ++++++++++++ firstSFML/Block.h | 184 ++++++++++++++++++++++++++++ firstSFML/Collider.h | 10 ++ firstSFML/Constants.h | 35 ++++++ firstSFML/Hero.h | 33 +++++ firstSFML/Mummy.h | 11 ++ firstSFML/firstSFML.cpp | 93 +++++++++++--- firstSFML/firstSFML.vcxproj | 24 ++++ firstSFML/firstSFML.vcxproj.filters | 26 ++++ firstSFML/level_1_blocks.txt | 24 ++++ firstSFML/pch.h | 16 ++- 12 files changed, 516 insertions(+), 27 deletions(-) create mode 100644 firstSFML/Actor.h create mode 100644 firstSFML/Block.h create mode 100644 firstSFML/Collider.h create mode 100644 firstSFML/Constants.h create mode 100644 firstSFML/Hero.h create mode 100644 firstSFML/Mummy.h create mode 100644 firstSFML/level_1_blocks.txt diff --git a/SFMLprog.sln b/SFMLprog.sln index d0b25f2..b8ea1a3 100644 --- a/SFMLprog.sln +++ b/SFMLprog.sln @@ -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 diff --git a/firstSFML/Actor.h b/firstSFML/Actor.h new file mode 100644 index 0000000..82d332b --- /dev/null +++ b/firstSFML/Actor.h @@ -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; + } +}; \ No newline at end of file diff --git a/firstSFML/Block.h b/firstSFML/Block.h new file mode 100644 index 0000000..2f674b9 --- /dev/null +++ b/firstSFML/Block.h @@ -0,0 +1,184 @@ +#pragma once +#include "pch.h" +#include +#include + +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(); + } +}; \ No newline at end of file diff --git a/firstSFML/Collider.h b/firstSFML/Collider.h new file mode 100644 index 0000000..9fb3e68 --- /dev/null +++ b/firstSFML/Collider.h @@ -0,0 +1,10 @@ +#pragma once +#include "pch.h" + +class Collider { + +public: + t_collided checkCollision(Block &obj1, Block &obj2) { + + } +}; \ No newline at end of file diff --git a/firstSFML/Constants.h b/firstSFML/Constants.h new file mode 100644 index 0000000..744a649 --- /dev/null +++ b/firstSFML/Constants.h @@ -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; + diff --git a/firstSFML/Hero.h b/firstSFML/Hero.h new file mode 100644 index 0000000..3cf982c --- /dev/null +++ b/firstSFML/Hero.h @@ -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(); + } +}; \ No newline at end of file diff --git a/firstSFML/Mummy.h b/firstSFML/Mummy.h new file mode 100644 index 0000000..bcc80bc --- /dev/null +++ b/firstSFML/Mummy.h @@ -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); + } + +}; \ No newline at end of file diff --git a/firstSFML/firstSFML.cpp b/firstSFML/firstSFML.cpp index f110fb7..7d40068 100644 --- a/firstSFML/firstSFML.cpp +++ b/firstSFML/firstSFML.cpp @@ -1,21 +1,78 @@ -// firstSFML.cpp : This file contains the 'main' function. Program execution begins and ends there. -// - #include "pch.h" +#include #include +#include "windows.h" + +using namespace std; + +int main() { + sf::Clock clock; + float elapsed; + + Block controller; + Block map[MAP_SIZE_X][MAP_SIZE_Y]; + controller.openMap(map, "level_1_blocks.txt"); + //Sleep(5000); + + sf::RenderWindow window(sf::VideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y), "King's Valley", sf::Style::Default); + window.setFramerateLimit(200); + + Hero player({ PLAYER_SIZE_X, PLAYER_SIZE_Y }); + player.setPos({ 200.f, 200.f }); + + Mummy bot1({ PLAYER_SIZE_X, PLAYER_SIZE_Y }); + bot1.setPos({ 200.f, 300.f }); + Mummy bot2({ PLAYER_SIZE_X, PLAYER_SIZE_Y }); + bot2.setPos({ 550.f, 200.f }); + + // Gravity variables: + const int groundHeight = 600; + + Block ground({ WINDOW_SIZE_X , WINDOW_SIZE_Y - groundHeight }); + ground.setPos({ 0, (float)groundHeight }); + + float i = 0; + while (window.isOpen()) { + cout << "_______New Lap________" << endl; + sf::Event _event; + // keypress detection + + // parameters + while (window.pollEvent(_event)) { + switch (_event.type) { + case sf::Event::Closed: + window.close(); + } + } + + player.controle(); + + player.annulateCollision(); // if won't be any collisions, value won't change fron 'no' + + player.checkCollision(ground); + player.checkCollision(bot1); + + window.clear(); + + bot1.drawTo(window); + //bot2.drawTo(window); + //controller.drawTo(window, map); + player.drawTo(window); + //ground.drawTo(window); + + /* + elapsed = clock.restart().asMilliseconds(); + gravity_speed = GRAVITY_SPEED_CONST * elapsed; + x_move_speed = X_MOVE_SPEED_CONST * elapsed; + jump_change_step = JUMP_CHANGE_STEP_CONST * elapsed; + cout << elapsed << endl; + */ + gravity_speed = GRAVITY_SPEED_CONST; + x_move_speed = X_MOVE_SPEED_CONST; + jump_change_step = JUMP_CHANGE_STEP_CONST; -int main() -{ - std::cout << "Hello World!\n"; -} - -// Run program: Ctrl + F5 or Debug > Start Without Debugging menu -// Debug program: F5 or Debug > Start Debugging menu - -// Tips for Getting Started: -// 1. Use the Solution Explorer window to add/manage files -// 2. Use the Team Explorer window to connect to source control -// 3. Use the Output window to see build output and other messages -// 4. Use the Error List window to view errors -// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project -// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file + window.display(); + + } + return 0; +} \ No newline at end of file diff --git a/firstSFML/firstSFML.vcxproj b/firstSFML/firstSFML.vcxproj index 0219e03..8168cce 100644 --- a/firstSFML/firstSFML.vcxproj +++ b/firstSFML/firstSFML.vcxproj @@ -91,10 +91,14 @@ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true pch.h + + Console true + + @@ -106,10 +110,13 @@ _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true pch.h + C:\home\bin\vslibrary\SFML-2.5.1\include Console true + C:\home\bin\vslibrary\SFML-2.5.1\lib + sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;%(AdditionalDependencies) @@ -123,12 +130,17 @@ WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true pch.h + + Console true true true + + + sfml-graphics.lib;sfml-window.lib;sfml-system.lib;%(AdditionalDependencies) @@ -142,16 +154,25 @@ NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true pch.h + C:\home\bin\vslibrary\SFML-2.5.1\include Console true true true + C:\home\bin\vslibrary\SFML-2.5.1\lib + sfml-graphics.lib;sfml-window.lib;sfml-system.lib;%(AdditionalDependencies) + + + + + + @@ -162,6 +183,9 @@ Create + + + diff --git a/firstSFML/firstSFML.vcxproj.filters b/firstSFML/firstSFML.vcxproj.filters index 7408420..492ab83 100644 --- a/firstSFML/firstSFML.vcxproj.filters +++ b/firstSFML/firstSFML.vcxproj.filters @@ -13,11 +13,32 @@ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + {3623aad4-d076-4434-9249-3907fa2db888} + Header Files + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + @@ -27,4 +48,9 @@ Source Files + + + Source Files\levels + + \ No newline at end of file diff --git a/firstSFML/level_1_blocks.txt b/firstSFML/level_1_blocks.txt new file mode 100644 index 0000000..24cfb32 --- /dev/null +++ b/firstSFML/level_1_blocks.txt @@ -0,0 +1,24 @@ +10000000000000000000000000000001 +10000000000000000000000000000001 +10000011111111111111111111000001 +11141110000000000000000001117111 +10003000000000000000000000060001 +10000300000000000000000000600001 +10000030000000000000000006000001 +11141112111000000000000151117111 +10003000000000000000000000060001 +10000300000000000000000000600001 +10000030000000000000000006000001 +10000003000000000000000060000001 +10000111211171188114111511111001 +10000000000600000000300000010001 +10000000006000000000030000010001 +10000000060000000000003000010001 +10001141511000000000011217111111 +10000003000000000000000060000001 +10000000300000111100000600000001 +10000000030000000000006000000001 +11010000003000000000060000000001 +10011100000300000000600000000111 +11111111111121111115111111111111 +88888888888888888888888888888888 \ No newline at end of file diff --git a/firstSFML/pch.h b/firstSFML/pch.h index b04e71e..cb6855b 100644 --- a/firstSFML/pch.h +++ b/firstSFML/pch.h @@ -1,14 +1,12 @@ -// Tips for Getting Started: -// 1. Use the Solution Explorer window to add/manage files -// 2. Use the Team Explorer window to connect to source control -// 3. Use the Output window to see build output and other messages -// 4. Use the Error List window to view errors -// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project -// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file - #ifndef PCH_H #define PCH_H -// TODO: add headers that you want to pre-compile here +#include +#include "Constants.h" + +#include "Block.h" +#include "Actor.h" +#include "Hero.h" +#include "Mummy.h" #endif //PCH_H