Камера

Помечено: 

В этой теме 2 ответа, 2 участника, последнее обновление  AnatoliiD 5 года/лет, 1 месяц назад.

Просмотр 3 сообщений - с 1 по 3 (из 3 всего)
  • Автор
    Сообщения
  • #5096

    AnatoliiD
    Участник
    Сообщений:1

    Зарегистрирован:
    27.02.2019

    Репутация:0

    Решил привязать камеру к персонажу. координаты персонажа камера получает от управления персонажа, а она двигается только по диагонали влево и вправо, а при нажатии вверх или вниз не работает. подскажите, как правильно привязать камеру.

    main.cpp

    #include <SFML/Graphics.hpp>
    #include <iostream>
    #include “map.h”
    #include “view.h”

    using namespace sf;

    class Player {
    private:
    float x, y = 0;
    public:
    float w, h, dx, dy, speed;
    int dir;
    String File;
    Image image;
    Texture texture;
    Sprite sprite;
    Player(String F, float X, float Y, float W, float H) {
    dx = 0; dy = 0; speed = 0; dir = 0;
    File = F;
    w = W; h = H;
    x = X; y = Y;
    image.loadFromFile(“images/” + File);
    image.createMaskFromColor(Color(255, 0, 0));
    texture.loadFromImage(image);
    sprite.setTexture(texture);
    sprite.setTextureRect(IntRect(0, 0, w, h));
    };

    void Update(float time) {
    switch (dir){
    case 0: dx = 0; dy = -speed; break; // up
    case 1: dx = 0; dy = speed; break; // down
    case 2: dx = -speed; dy = 0; break; // left
    case 3: dx = speed; dy = 0; break; // rigth
    }

    x += dx * time;
    y += dy * time;

    speed = 0;
    sprite.setPosition(x, y);
    }

    float getCoordinateX()//забираем координату Х
    {
    return x;
    }
    float getCoordinateY()// забираем координату Y
    {
    return x;
    }
    };
    int main()
    {
    RenderWindow window(VideoMode(1280, 720), “Yolly”);
    camera.reset(FloatRect(0, 0, 600, 600)); // размер камеры при ее создании

    Image image_map;
    image_map.loadFromFile(“images/map.png”);
    Texture texture_map;
    texture_map.loadFromImage(image_map);
    Sprite sprite_map;
    sprite_map.setTexture(texture_map);

    Clock clock; // создаем обьект время

    Player Hero(“hero.png”, 100, 100, 83.0, 80.0);

    while (window.isOpen())
    {

    float time = clock.getElapsedTime().asMicroseconds(); //дать прошедшее время в микросекундах
    clock.restart(); //перезагружает время
    time = time / 800; //скорость игры

    Event event;

    while (window.pollEvent(event))
    {
    if (event.type == Event::Closed)
    window.close();
    }

    /////////////////////////////////////////////Управление персонажем/////////////////////////////////////////////
    if (Keyboard::isKeyPressed(Keyboard::W)) {
    Hero.dir = 0; Hero.speed = 0.5;
    getPlayerCoordinateForView(Hero.getCoordinateX(), Hero.getCoordinateY());
    }
    if (Keyboard::isKeyPressed(Keyboard::S)) {
    Hero.dir = 1; Hero.speed = 0.5;
    getPlayerCoordinateForView(Hero.getCoordinateX(), Hero.getCoordinateY());
    }
    if (Keyboard::isKeyPressed(Keyboard::A)) {
    Hero.dir = 2; Hero.speed = 0.5;
    Hero.sprite.setTextureRect(IntRect(83, 0, -83, 80));
    getPlayerCoordinateForView(Hero.getCoordinateX(), Hero.getCoordinateY());
    }
    if (Keyboard::isKeyPressed(Keyboard::D)) {
    Hero.dir = 3; Hero.speed = 0.5;
    Hero.sprite.setTextureRect(IntRect(0, 0, 83, 80));
    getPlayerCoordinateForView(Hero.getCoordinateX(), Hero.getCoordinateY());
    }

    Hero.Update(time);
    window.setView(camera);

    window.clear();

    ///////////////////////////////////////////////Рисуем карту////////////////////////////////////////////////////////
    for (int i = 0; i < H_map; i++)
    for (int j = 0; j < W_map; j++) {
    if (TileMap[i][j] == ‘ ‘) sprite_map.setTextureRect(IntRect(0, 0, 30, 30));
    if (TileMap[i][j] == ‘U’) sprite_map.setTextureRect(IntRect(30, 0, 31, 31));
    if (TileMap[i][j] == ‘L’) sprite_map.setTextureRect(IntRect(60, 0, 31, 31));
    if (TileMap[i][j] == ‘D’) sprite_map.setTextureRect(IntRect(90, 0, 30, 30));
    if (TileMap[i][j] == ‘R’) sprite_map.setTextureRect(IntRect(120, 0, 30, 30));
    if (TileMap[i][j] == ‘X’) sprite_map.setTextureRect(IntRect(128, 0, 320, 30));

    sprite_map.setPosition(j * 20, i * 20);

    window.draw(sprite_map);
    }
    window.draw(Hero.sprite); // выводим спрайт на экран
    window.display();
    }

    return 0;
    }

    view.h

    #include <SFML/Graphics.hpp>

    using namespace sf;

    View camera;

    View getPlayerCoordinateForView(float x, float y) {

    camera.setCenter(x, y);

    return camera;
    }

    #5097

    freshik
    Участник
    Сообщений:1

    Зарегистрирован:
    28.02.2019

    Репутация:0

    Попробуй просто скопировать  код из урока, если поможет, тогда смотри внимательнее свой код, очень часто не правильно задается переменная или пропускается “;”

    #5098

    AnatoliiD
    Участник
    Сообщений:1

    Зарегистрирован:
    27.02.2019

    Репутация:0

    делал, не помогает

Просмотр 3 сообщений - с 1 по 3 (из 3 всего)

Для ответа в этой теме необходимо авторизоваться.