/
Platform.cpp
91 lines (79 loc) · 3.16 KB
/
Platform.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "Platform.h"
#include "Globals.h"
// TODO: Doðrudan topdown sýnýfýndan alýndý düzenlenmesi gerekiyor.
/* Hareket vektörünü birim vektör olarak günceller. */
sf::Vector2f Platform::normalize(sf::Vector2f source){
float length = sqrt((source.x * source.x) + (source.y * source.y));
if(length != 0)
return sf::Vector2f(source.x / length, source.y / length);
else
return source;
}
Platform::Platform(Entity* entity) : Behavior(entity){
}
// TODO: feature TopDown davranýþýna sahip varlýk katý objelerin içinden geçmemeli.
bool Platform::checkCollision(Entity& e1, Entity& e2){
/*sf::FloatRect obj1 = e1.getSprite().getGlobalBounds();
obj1.width *= e1.getSprite().getScale().x;
obj1.height *= e1.getSprite().getScale().y;
sf::FloatRect obj2 = e2.getSprite().getGlobalBounds();
obj2.width *= e2.getSprite().getScale().x;
obj2.height *= e2.getSprite().getScale().y;
return obj1.intersects(obj2);*/
return e1.getSprite().getGlobalBounds().intersects(e2.getSprite().getGlobalBounds());
}
bool Platform::checkCollision(sf::Sprite e1, sf::Sprite e2){
/*sf::FloatRect obj1 = e1.getGlobalBounds();
obj1.width *= e1.getScale().x;
obj1.height*= e1.getScale().y;
sf::FloatRect obj2 = e2.getGlobalBounds();
obj2.width *= e2.getScale().x;
obj2.height *= e2.getScale().y;
return obj1.intersects(obj2);*/
//std::cout << "t1x: " << e1.getPosition().x << "\t e1y: " << e1.getPosition().y << "\t e2x: " << e2.getPosition().x << "\t e2y: " << e2.getPosition().y << std::endl;
return e1.getGlobalBounds().intersects(e2.getGlobalBounds());
}
void Platform::update(float dt){
handlePlayerInput();
/*entity->collisionShape.setSize(sf::Vector2f(entity->texture->getSize().x, entity->texture->getSize().y));
entity->collisionShape.setPosition(entity->shape.getPosition());
entity->collisionShape.move(entity->movement * dt * entity->movementSpeed);
if( entity.ge)*/
std::vector<Entity*> currentEntities = *(globals.getCurrentEntities());
entity->movement.y += 0.020f;
int size = currentEntities.size();
for(int i = 0; i < size; i++){
if(currentEntities[i]->isSolid && checkCollision(currentEntities[i]->getSprite(), this->getNextFrameSprite(dt))){
sf::Vector2f s1 = currentEntities[i]->getSprite().getPosition();
sf::Vector2f s2 = this->getNextFrameSprite(dt).getPosition();
if(abs(s1.x - s2.x) > abs(s1.y - s2.y)){
this->entity->movement.x = 0;
} else{
this->entity->movement.y = 0;
canJump = true;
}
}
}
entity->move(entity->movement * dt * entity->movementSpeed);
}
void Platform::handlePlayerInput(){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
entity->movement.x = 1;
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
entity->movement.x = -1;
} else{
entity->movement.x = 0;
}
if(canJump && sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
entity->movement.y = -1;
canJump = false;
}
}
sf::Sprite Platform::getNextFrameSprite(float dt){
sf::Sprite nextSprite(this->entity->getSprite());
sf::Vector2f nextPos(this->entity->getPosition());
nextPos.x += this->entity->movement.x * dt * this->entity->movementSpeed;
nextPos.y += this->entity->movement.y * dt * this->entity->jumpStrength;
nextSprite.setPosition(nextPos);
return nextSprite;
}