#include #include #include #include "view.h" #include "tinyxml.h" #include "level.h" #include #include using namespace sf; using namespace std; ///////////////////////////// класс Родитель //////////////////////////////// class Entity { public: vector obj;//вектор объектов карты float dx, dy, x, y, speed, moveTimer; int w, h, health; bool life, isMove, onGround; Texture texture; Sprite sprite; String name; Entity(String Name, Image &image, float X, float Y, int W, int H) { x = X; y = Y; w = W; h = H; name = Name; moveTimer = 0; speed = 0; health = 100; dx = 0; dy = 0; life = true; onGround = false; isMove = false; texture.loadFromImage(image); sprite.setTexture(texture); sprite.setOrigin(w / 2, h / 2); } FloatRect getRect() //ф-ция получения прямоугольника. его коорд,размеры (шир,высот). { return FloatRect(x, y, w, h); } }; ////////////////////////////// класс Игрока //////////////////////////////// class Player : public Entity { public: enum { left, right, up, down, jump, stay } state; int playerScore; Player(String Name, Image &image, Level &lvl, float X, float Y, int W, int H) : Entity(Name, image, X, Y, W, H) { obj = lvl.GetAllObjects(); playerScore = 0; state = stay; if (name == "Player1") { sprite.setTextureRect(IntRect(12, 0, w, h)); } } void control() { if (Keyboard::isKeyPressed) { if (Keyboard::isKeyPressed(Keyboard::A)) { state = left; speed = 0.1; } if (Keyboard::isKeyPressed(Keyboard::D)) { state = right; speed = 0.1; } if (Keyboard::isKeyPressed(Keyboard::W) && (onGround)) { state = jump; dy = -0.6; onGround = false; } if (Keyboard::isKeyPressed(Keyboard::S)) { state = down; } } } void update(float time) //функция "оживления" объекта класса. update - обновление. принимает в себя время SFML , вследствие чего работает бесконечно, давая персонажу движение. { control(); switch (state) { case right: dx = speed; break; case left: dx = -speed; break; case up: break; case down: break; case jump: break; case stay: break; } x += dx*time;//то движение из прошлого урока. наше ускорение на время получаем смещение координат и как следствие движение checkCollisionWithMap(dx, 0); y += dy*time; checkCollisionWithMap(0, dy); sprite.setPosition(x + w / 2, y + h / 2); if (health <= 0) { life = false; } if (!isMove) speed = 0; if (life) { setPlayerCoordinateForView(x, y); } dy = dy + 0.0015 * time; } void checkCollisionWithMap(float Dx, float Dy)//ф ция проверки столкновений с картой { for (int i = 0; i < obj.size(); ++i) if (getRect().intersects(obj[i].rect)) { if (obj[i].name == "solid") { if (Dy > 0) { y = obj[i].rect.top - h; dy = 0; onGround = true; } if (Dy < 0) { y = obj[i].rect.top + obj[i].rect.height; dy = 0; } if (Dx > 0) { x = obj[i].rect.left - w; } if (Dx < 0) { x = obj[i].rect.left + obj[i].rect.width; } } } } }; ////////////////////////////// класс Противника //////////////////////////////// class Enemy : public Entity { public: Enemy(String Name, Image &image, Level &lvl, float X, float Y, int W, int H) : Entity(Name, image, X, Y, W, H) { obj = lvl.GetObjects("solid"); if (name == "Enemy1") { sprite.setTextureRect(IntRect(40, 40, -w, h)); dx = 0.1; } } void checkCollisionWithMap(float Dx, float Dy)//ф ция проверки столкновений с картой { for (int i = 0; i < obj.size(); ++i) if (getRect().intersects(obj[i].rect)) { if (obj[i].name == "solid") { if (Dy > 0) { y = obj[i].rect.top - h; dy = 0; onGround = true; } if (Dy < 0) { y = obj[i].rect.top + obj[i].rect.height; dy = 0; } if (Dx > 0) { x = obj[i].rect.left - w; dx = -0.1; sprite.scale(-1, 1); } if (Dx < 0) { x = obj[i].rect.left + obj[i].rect.width; dx = 0.1; sprite.scale(-1, 1); } } } } void update(float time) { if (name == "Enemy1") { checkCollisionWithMap(dx, 0); x += dx*time; sprite.setPosition(x + w / 2, y + h / 2); if (health <= 0) { life = false; } } } };