/
Obstacle.cpp
51 lines (45 loc) · 1.3 KB
/
Obstacle.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
/* --------------------------------------------------------------- //
Copyright (C) 2010 Manuel Riecke
Licensed under the terms of the GNU GPL 2.0
See Copyright/Code.txt for details.
// --------------------------------------------------------------- */
#include "Obstacle.h"
#include <SFML/Graphics/Image.hpp>
#include "Player.h"
#include "Enemy.h"
#include <iostream>
Obstacle::Obstacle(float x, float y, const sf::Image& image)
{
mSprite.SetImage(image);
mSprite.SetPosition(x, y);
mRect = sf::FloatRect(x, y, x+image.GetWidth(), y+image.GetHeight());
mHitPlayer = false;
}
void Obstacle::update(float delta, float xSpeed)
{
mSprite.Move(delta*xSpeed, 0);
mRect.Offset(delta*xSpeed, 0);
}
bool Obstacle::hits(const sf::FloatRect& other)
{
return mRect.Intersects(other);
}
void Obstacle::hitCheck(Player* player)
{
if(!mHitPlayer)
{
const sf::Vector2f& pos=player->getSprite().GetPosition();
if(hits(sf::FloatRect(pos.x, pos.y-10, pos.x+5, pos.y-5)) && hits(sf::FloatRect(pos.x, pos.y+10, pos.x+5, pos.y+15)))
{
// Bam, Headshot.
player->doLife(-1.1f);
mHitPlayer=true;
}
}
}
void Obstacle::hitCheck(Enemy* enemy)
{
const sf::Vector2f& pos=enemy->getSprite().GetPosition();
if(hits(sf::FloatRect(pos.x, pos.y, pos.x+5, pos.y+5)))
enemy->destroy();
}