SFML и C++ Уроки \ Разработка игр › Форумы › SFML System, Window и другие вопросы › Реализация взаимодействия с картой, написаной через VertexArray
Помечено: Взаимодействие с картой
В этой теме 0 ответов, 1 участник, последнее обновление newUser11 8 года/лет, 8 мес. назад.
Просмотр 1 сообщения - с 1 по 1 (всего 1)
-
АвторСообщения
-
Всем привет. Возникла потребность перерисовать карту через VertexArray, в итоге взаимодействие персонажа с картой нужно переделывать, до меня не доходит как, помогите плиз:) Вот код и ресурсы.
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165#include <SFML/Graphics.hpp>#include "TileMap.h"#include "view.h"#include "player.h"#include <SFML/Audio.hpp>#include <sstream>int main(){// create the windowsf::RenderWindow window(sf::VideoMode(640, 480), "Game1");// define the level with an array of tile indicesconst int level[] ={402, 402, 402, 402, 402, 402, 402, 402, 402, 402, 402, 402,402, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 402,402, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 402,402, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 402,402, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 402,402, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 402,402, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 402,402, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 402,402, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 402,402, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 402,402, 402, 402, 402, 402, 402, 402, 402, 402, 402, 402, 402,};// create the tilemap from the level definitionTileMap map;if (!map.load("images.duckduckgo.com.png", sf::Vector2u(32, 32), level, 12, 11))return -1;Music music;music.openFromFile("Crate_Entertainment_-_The_Captain.ogg");music.setVolume(30);music.setLoop(true);music.play();Font font;font.loadFromFile("CyrilicOld.ttf");Text text("", font, 15);text.setColor(Color::Green);Text text1("", font, 15);text1.setColor(Color::Green);Text text3("", font, 15);text3.setColor(Color::Green);Player p("mage.png", 250, 250, 34.0, 50.0);float CurrentFrame = 0;Clock clock;Clock clock1;char atkPosition = 's';// run the main loopwhile (window.isOpen()){float time = clock.getElapsedTime().asMicroseconds();long int time1 = clock1.getElapsedTime().asSeconds();clock.restart();time = time / 500;// handle eventssf::Event event;while (window.pollEvent(event)){if(event.type == sf::Event::Closed)window.close();}float coordinatePlayerX, coordinatePlayerY = 0;coordinatePlayerX = p.getplayercoordinateX();coordinatePlayerY = p.getplayercoordinateY();p.update(time);if (Keyboard::isKeyPressed(Keyboard::A) || Keyboard::isKeyPressed(Keyboard::Left)){p.dir = 1; p.speed = 0.1;atkPosition = 'l';CurrentFrame += 0.005*time;if (CurrentFrame > 4) CurrentFrame -= 4;p.sprite.setTextureRect(IntRect(64 * int(CurrentFrame)+12, 384, 34, 38));}if (Keyboard::isKeyPressed(Keyboard::D) || Keyboard::isKeyPressed(Keyboard::Right)){p.dir = 0; p.speed = 0.1;atkPosition = 'r';CurrentFrame += 0.005*time;if (CurrentFrame > 4) CurrentFrame -= 4;p.sprite.setTextureRect(IntRect(64 * int(CurrentFrame), 128, 34, 38));}if (Keyboard::isKeyPressed(Keyboard::W) || Keyboard::isKeyPressed(Keyboard::Up)){p.dir = 3; p.speed = 0.1;atkPosition = 't';CurrentFrame += 0.005*time;if (CurrentFrame > 4) CurrentFrame -= 4;p.sprite.setTextureRect(IntRect(64 * int(CurrentFrame), 0, 32, 50));}if (Keyboard::isKeyPressed(Keyboard::S) || Keyboard::isKeyPressed(Keyboard::Down)){p.dir = 2; p.speed = 0.1;atkPosition = 'd';CurrentFrame += 0.005*time;if (CurrentFrame > 4) CurrentFrame -= 4;p.sprite.setTextureRect(IntRect(64 * int(CurrentFrame), 255, 32, 38));}if((Keyboard::isKeyPressed(Keyboard::Space)&&(atkPosition == 'r'))){p.sprite.setTextureRect(IntRect(450, 192, 43, 40));}if((Keyboard::isKeyPressed(Keyboard::Space)&&(atkPosition == 'l'))){p.sprite.setTextureRect(IntRect(450, 320, 43, 40));}if((Keyboard::isKeyPressed(Keyboard::Space)&&(atkPosition == 't'))){p.sprite.setTextureRect(IntRect(385, 0, 32, 50));}if((Keyboard::isKeyPressed(Keyboard::Space)&&(atkPosition == 'd'))){p.sprite.setTextureRect(IntRect(450, 255, 34, 45));}getplayercoordinateforview(coordinatePlayerX, coordinatePlayerY);p.update(time);window.setView(view);window.clear();std::ostringstream playerScoreString;playerScoreString << p.playerScore;text.setString("Score: " + playerScoreString.str());text.setPosition(p.getplayercoordinateX() - 20, p.getplayercoordinateY() - 40);window.draw(text);std::ostringstream playerHealth;playerHealth << p.playerHealth;text1.setString("Health: " + playerHealth.str());text1.setPosition(p.getplayercoordinateX() - 20, p.getplayercoordinateY() - 100);std::ostringstream Gametime;Gametime << time1;text3.setString("Time: " + Gametime.str());text3.setPosition(p.getplayercoordinateX() - 20, p.getplayercoordinateY() - 70);window.clear();window.draw(map);window.draw(text);window.draw(text1);window.draw(text3);window.draw(p.sprite);window.display();}return 0;}C++1234567891011121314151617181920212223242526272829303132333435363738#include "player.h"Player::Player(String F, float X, float Y, float W, float H){File = F;dx = 0;dy = 0;speed = 0;y = 0;w = W; h = H;playerScore = 0;playerHealth = 100;image.loadFromFile("images/" + File);image.createMaskFromColor(Color(252, 0, 252));texture.setSmooth(true);texture.loadFromImage(image);sprite.setTexture(texture);x = X; y = Y;sprite.setTextureRect(IntRect(0, 0, w, h));}void Player::update(float time){switch (dir){case 0: dx = speed; dy = 0; break;case 1: dx = -speed; dy = 0; break;case 2: dx = 0; dy = speed; break;case 3: dx = 0; dy = -speed; break;}x += dx*time;y += dy*time;speed = 0;sprite.setPosition(x,y);}C++123456789101112131415161718192021222324252627#include <SFML\Graphics.hpp>using namespace sf;class Player{private: float x, y;public:float w, h, dx, dy, speed;int dir, playerScore, playerHealth;String File;Image image;Texture texture;Sprite sprite;Player(String F, float X, float Y, float W, float H);void update(float time);float getplayercoordinateX(){return x;}float getplayercoordinateY(){return y;}};C++1234567891011121314151617181920212223242526#include <SFML/Graphics.hpp>class TileMap : public sf::Drawable, public sf::Transformable{public:bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height);int tileNumber;private:virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const{// apply the transformstates.transform *= getTransform();// apply the tileset texturestates.texture = &m_tileset;// draw the vertex arraytarget.draw(m_vertices, states);}sf::VertexArray m_vertices;sf::Texture m_tileset;};C++12345678910111213141516171819202122232425262728293031323334353637383940414243#include "TileMap.h"bool TileMap::load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height){int koef_of_size = 3;// load the tileset textureif (!m_tileset.loadFromFile(tileset))return false;// resize the vertex array to fit the level sizem_vertices.setPrimitiveType(sf::Quads);m_vertices.resize(width * height * 4);// populate the vertex array, with one quad per tilefor (unsigned int i = 0; i < width; ++i)for (unsigned int j = 0; j < height; ++j){// get the current tile numbertileNumber = tiles[i + j * width];// find its position in the tileset textureint tu = tileNumber % (m_tileset.getSize().x / tileSize.x);int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);// get a pointer to the current tile's quadsf::Vertex* quad = &m_vertices[(i + j * width) * 4];// define its 4 cornersquad[0].position = sf::Vector2f(i * tileSize.x * koef_of_size, j * tileSize.y * koef_of_size);quad[1].position = sf::Vector2f((i + 1) * tileSize.x * koef_of_size, j * tileSize.y * koef_of_size);quad[2].position = sf::Vector2f((i + 1) * tileSize.x * koef_of_size, (j + 1) * tileSize.y * koef_of_size);quad[3].position = sf::Vector2f(i * tileSize.x * koef_of_size, (j + 1) * tileSize.y * koef_of_size);// define its 4 texture coordinatesquad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);}return true;}C++12345678910111213141516#include <SFML\Graphics.hpp>using namespace sf;sf::View view;//объявили sfml объект "вид", который и является камеройvoid getplayercoordinateforview(float x, float y){float tempX = x; float tempY = y;//считываем коорд игрока и проверяем их, чтобы убрать краяif (x < 320) tempX = 320;//убираем из вида левую сторонуif (y < 240) tempY = 240;//верхнюю сторонуif (y > 554) tempY = 554;//нижнюю сторонуview.setCenter(tempX, tempY); //следим за игроком, передавая его координаты.}Вложения:
You must be logged in to view attached files. -
АвторСообщения
Просмотр 1 сообщения - с 1 по 1 (всего 1)
Для ответа в этой теме необходимо авторизоваться.