forked from hminoura/factory
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.cpp
56 lines (46 loc) · 780 Bytes
/
Ball.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
#include "Ball.h"
#include "Rand.h"
#include "BallApp.h"
#include "TextureManager.h"
#include <iostream>
using namespace std;
Ball::Ball()
{
x = y = 0;
vx = vy = 0;
ax = ay = 0;
}
Ball::~Ball()
{
}
void Ball::init()
{
x = BallApp::window.getSize().x / 2;
y = BallApp::window.getSize().y / 2;
vx = Rand::range(-300, 300);
vy = Rand::range(-300, 300);
ax = 0;
ay = 0;
rap.reset();
}
void Ball::update()
{
double dt = rap.get();
rap.reset();
vx += ax * dt;
vy += ay * dt;
x += vx * dt;
y += vy * dt;
if (x < 0 || x > BallApp::window.getSize().x - 1)
vx = -vx;
if (y < 0 || y > BallApp::window.getSize().y - 1)
vy = -vy;
}
void Ball::draw()
{
sprite.setPosition(x, y);
BallApp::window.draw(sprite);
}
void Ball::clean()
{
}