]> git.localhorst.tv Git - space.git/blob - src/app/Application.cpp
added autopilot that sucks
[space.git] / src / app / Application.cpp
1 #include "Application.h"
2
3 #include "../graphics/Canvas.h"
4 #include "../graphics/Color.h"
5
6
7 namespace space {
8
9 Application::Application(Canvas &c)
10 : canvas(c)
11 , univ(Vector<int>(10, 10), Vector<int>(10, 10), Vector<int>(10, 10))
12 , focus(Vector<float>(500, 500), 500)
13 , cam(c.Size(), focus.Pos())
14 , controlled(univ.AddShip(Ship()))
15 , autopilot(*controlled, focus.Pos())
16 , apEnabled(false)
17 , last(SDL_GetTicks())
18 , running(false)
19 , paused(false) {
20
21 }
22
23
24 void Application::Run() {
25         running = true;
26         while (running) {
27                 Uint32 now = SDL_GetTicks();
28                 int delta = now - last;
29                 Loop(delta);
30                 last = now;
31         }
32 }
33
34
35 void Application::Loop(int delta) {
36         HandleEvents();
37         if (!paused) {
38                 Update(delta);
39         }
40         Render();
41         canvas.Present();
42 }
43
44
45 void Application::HandleEvents() {
46         SDL_Event event;
47         while (SDL_PollEvent(&event)) {
48                 switch (event.type) {
49                         case SDL_QUIT:
50                                 running = false;
51                                 break;
52                         case SDL_WINDOWEVENT:
53                                 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
54                                         cam.Resize(event.window.data1, event.window.data2);
55                                 }
56                                 break;
57                         case SDL_KEYDOWN:
58                                 if (!event.key.repeat) {
59                                         OnKeyDown(event.key);
60                                 }
61                                 break;
62                         case SDL_KEYUP:
63                                 if (!event.key.repeat) {
64                                         OnKeyUp(event.key);
65                                 }
66                                 break;
67                         default:
68                                 // skip event
69                                 break;
70                 }
71         }
72 }
73
74 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
75         switch (e.keysym.sym) {
76                 case SDLK_UP:
77                         focus.MoveUp();
78                         break;
79                 case SDLK_DOWN:
80                         focus.MoveDown();
81                         break;
82                 case SDLK_LEFT:
83                         focus.MoveLeft();
84                         break;
85                 case SDLK_RIGHT:
86                         focus.MoveRight();
87                         break;
88                 case SDLK_w:
89                         control.y -= 1;
90                         break;
91                 case SDLK_s:
92                         control.y += 1;
93                         break;
94                 case SDLK_a:
95                         control.x -= 1;
96                         break;
97                 case SDLK_d:
98                         control.x += 1;
99                         break;
100                 case SDLK_z:
101                         cam.StartZoom();
102                         break;
103                 case SDLK_x:
104                         cam.StartShrink();
105                         break;
106                 case SDLK_c:
107                         apEnabled = !apEnabled;
108                         break;
109                 case SDLK_p:
110                         paused = !paused;
111                         break;
112                 default:
113                         break;
114         }
115 }
116
117 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
118         switch (e.keysym.sym) {
119                 case SDLK_UP:
120                         focus.StopUp();
121                         break;
122                 case SDLK_DOWN:
123                         focus.StopDown();
124                         break;
125                 case SDLK_LEFT:
126                         focus.StopLeft();
127                         break;
128                 case SDLK_RIGHT:
129                         focus.StopRight();
130                         break;
131                 case SDLK_w:
132                         control.y += 1;
133                         break;
134                 case SDLK_s:
135                         control.y -= 1;
136                         break;
137                 case SDLK_a:
138                         control.x += 1;
139                         break;
140                 case SDLK_d:
141                         control.x -= 1;
142                         break;
143                 case SDLK_z:
144                         cam.StopZoom();
145                         break;
146                 case SDLK_x:
147                         cam.StopShrink();
148                         break;
149                 default:
150                         break;
151         }
152 }
153
154
155 void Application::Update(int dt) {
156         const float delta = dt / 1e3;
157         if (apEnabled) {
158                 autopilot.Update(delta);
159         } else {
160                 controlled->rotThrottle = control.x;
161                 controlled->linThrottle = -control.y;
162         }
163         cam.Update(delta);
164         univ.Update(delta);
165         focus.SetSpeed(500 / cam.Zoom());
166         focus.Update(delta);
167 }
168
169
170 void Application::Render() {
171         constexpr Color background(0x00, 0x00, 0x00);
172         constexpr Color univGrid(0xEE, 0xEE, 0xEE);
173         constexpr Color secGrid(0x77, 0x77, 0x77);
174         constexpr Color entityColor(0x00, 0xAA, 0xAA);
175         constexpr Color focusColor(0xFA, 0xFA, 0x00);
176
177         canvas.SetColor(background);
178         canvas.Fill();
179
180         canvas.Grid2(
181                 cam.ToScreen(Vector<float>(0, 0)),
182                 cam.ToScale(univ.size * univ.secSize * univ.areaSize),
183                 cam.ToScale(univ.areaSize),
184                 univ.secSize,
185                 secGrid,
186                 univGrid);
187
188         canvas.SetColor(focusColor);
189         canvas.Cross(cam.ToScreen(focus.Pos()), 15);
190
191         canvas.SetColor(entityColor);
192         for (const Ship &s : univ.Ships()) {
193                 const Vector<float> direction = s.Dir();
194                 const Vector<int> position = cam.ToScreen(s.pos);
195                 const Vector<int> nose = position + Vector<int>(direction * 15.0f);
196                 const Vector<int> left = position + Vector<int>((Rotate90(direction) * 8.0f) - (direction * 4.0f));
197                 const Vector<int> right = position + Vector<int>((Rotate270(direction) * 8.0f) - (direction * 4.0f));
198                 canvas.Line(position, nose);
199                 canvas.Quad(nose, left, position, right);
200         }
201
202         autopilot.Render(canvas, cam);
203 }
204
205 }