-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathAsteroidField.cpp
88 lines (71 loc) · 2.62 KB
/
AsteroidField.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
/* AsteroidField.cpp
Copyright (c) 2015 by Michael Zahniser
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include "AsteroidField.h"
#include "SpriteSet.h"
#include "System.h"
#include <QPainter>
#include <QString>
#include <cmath>
using namespace std;
void AsteroidField::Set(const System *system)
{
asteroids.clear();
if(!system)
return;
for(const System::Asteroid &it : system->Asteroids())
{
QPixmap sprite = SpriteSet::Get("asteroid/" + it.type + "/spin-00");
for(int i = 0; i < it.count; ++i)
{
double angle = (rand() % 6283) * .001;
double velocity = (rand() % 1000) * (.001 * it.energy);
asteroids.push_back({
QVector2D(rand() % 4096 - 2048, rand() % 4096 - 2048),
QVector2D(velocity * sin(angle), velocity * cos(angle)),
sprite});
}
}
}
void AsteroidField::Step()
{
for(Asteroid &asteroid : asteroids)
{
asteroid.position += asteroid.velocity;
if(asteroid.position.x() < -2048.)
asteroid.position += QVector2D(4096., 0.);
if(asteroid.position.x() >= 2048.)
asteroid.position -= QVector2D(4096., 0.);
if(asteroid.position.y() < -2048.)
asteroid.position += QVector2D(0., 4096.);
if(asteroid.position.y() >= 2048.)
asteroid.position -= QVector2D(0., 4096.);
}
}
void AsteroidField::Draw(QPainter &painter, const QRectF &bounds) const
{
int firstX = round(bounds.left() / 4096.);
int lastX = round(bounds.right() / 4096.);
int firstY = round(bounds.top() / 4096.);
int lastY = round(bounds.bottom() / 4096.);
for(int y = firstY; y <= lastY; ++y)
for(int x = firstX; x <= lastX; ++x)
{
QVector2D offset(x * 4096., y * 4096.);
for(const Asteroid &asteroid : asteroids)
{
QPointF thisOffset = (asteroid.position + offset).toPointF();
painter.translate(thisOffset);
painter.scale(.5, .5);
painter.drawPixmap(QPointF(), asteroid.sprite);
painter.scale(2., 2.);
painter.translate(-thisOffset);
}
}
}