-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
78 lines (65 loc) · 1.84 KB
/
Player.cpp
File metadata and controls
78 lines (65 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.h"
Player::Player(){}
Player::~Player()
{
if (animation != nullptr)
delete animation;
}
void Player::create(sf::Texture* texture, sf::Vector2u imageCount, float switchTime, float speed)
{
this->speed = speed;
row = 0;
body.setSize(sf::Vector2f(float(texture->getSize().x / imageCount.x), float(texture->getSize().y / imageCount.y)));
body.setOrigin(body.getSize().x / 2, body.getSize().y / 2);
body.setPosition(100.0f, 100.0f);
body.setTexture(texture);
animation = new Animation(texture, imageCount, switchTime);
}
void Player::update(float deltaTime)
{
sf::Vector2f movement(0.0f, 0.0f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
movement.x -= speed * deltaTime;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
movement.x += speed * deltaTime;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
movement.y += speed * deltaTime;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
movement.y -= speed * deltaTime;
if (movement.y > 0.0f)
{
row = 0;
}
if (movement.y < 0.0f)
{
row = 1;
}
if (movement.x != 0.0f)
{
row = 2;
isFaceRight = (movement.x > 0.0f);
}
if ((movement.x != 0.0f) && (movement.y != 0.0f))
{
movement.x *= 0.75; // Åñëè íå ó÷èòûâàòü, òî îêàæåòñÿ,
movement.y *= 0.75; // ÷òî ïî äèàãîíàëè èäòè íàìíîãî áûñòðåå
} // Ïóñòü sqrt(0.5) ~= 0.75
isStanding = ((movement.x == 0.0f) && (movement.y == 0.0f)); // TODO: ñëåâà ïîÿâëÿåòñÿ ïèêñåëü
animation->update(row, deltaTime, isFaceRight, isStanding); // Åñëè ïåðñîíàæ ñòîèò, òî ïåðâûé ñïðàéò
body.setTextureRect(animation->uvRect);
body.move(movement);
}
void Player::draw(sf::RenderWindow& window)
{
window.draw(body);
}
sf::Vector2f Player::getPosition()
{
return body.getPosition();
}
Collider Player::getCollider()
{
return Collider(body);
}