2021-12-11 21:32:48 +01:00
|
|
|
#include "balls.h"
|
|
|
|
#include "game.h"
|
|
|
|
|
2023-03-14 21:45:20 +01:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
struct ball spaceship;
|
|
|
|
double spaceship_thrust = 0;
|
|
|
|
int spaceship_thrust_countdown = 0;
|
|
|
|
int spaceship_thrust_init = 50;
|
|
|
|
|
2023-03-14 21:45:20 +01:00
|
|
|
void spaceship_init_state() {
|
|
|
|
spaceship.position.x = width / 2;
|
|
|
|
spaceship.position.y = height / 2;
|
2021-12-11 21:32:48 +01:00
|
|
|
spaceship.radius = 30;
|
2022-06-01 11:09:35 +02:00
|
|
|
spaceship.velocity.x = 0;
|
|
|
|
spaceship.velocity.y = 0;
|
2021-12-11 21:32:48 +01:00
|
|
|
spaceship.angle = 0;
|
|
|
|
spaceship.v_angle = 0;
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:45:20 +01:00
|
|
|
void spaceship_update_state() {
|
2021-12-11 21:32:48 +01:00
|
|
|
if (spaceship_thrust > 0) {
|
2023-03-14 21:45:20 +01:00
|
|
|
vec2d f{cos(spaceship.angle) * spaceship_thrust * 4.0,
|
|
|
|
sin(spaceship.angle) * spaceship_thrust * 4.0};
|
2021-12-11 21:32:48 +01:00
|
|
|
|
2023-03-14 21:45:20 +01:00
|
|
|
spaceship.position += delta * delta * f / 2.0;
|
|
|
|
spaceship.velocity += delta * f;
|
|
|
|
if (spaceship_thrust_countdown > 0)
|
|
|
|
--spaceship_thrust_countdown;
|
|
|
|
else
|
|
|
|
spaceship_thrust = 0;
|
2021-12-11 21:32:48 +01:00
|
|
|
}
|
|
|
|
ball_update_state(&spaceship);
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:45:20 +01:00
|
|
|
void spaceship_draw(cairo_t* cr) {
|
2021-12-11 21:32:48 +01:00
|
|
|
static const double one_over_sqrt_2 = 0.70710678118654752440;
|
|
|
|
cairo_save(cr);
|
|
|
|
cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
|
2022-06-01 11:09:35 +02:00
|
|
|
cairo_translate(cr, spaceship.position.x, spaceship.position.y);
|
2021-12-11 21:32:48 +01:00
|
|
|
cairo_rotate(cr, spaceship.angle);
|
2023-03-14 21:45:20 +01:00
|
|
|
cairo_arc(cr, 0, 0, spaceship.radius, 0, 2 * M_PI);
|
2021-12-11 21:32:48 +01:00
|
|
|
cairo_stroke(cr);
|
|
|
|
cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);
|
2023-03-14 21:45:20 +01:00
|
|
|
cairo_move_to(cr, 0, 0);
|
|
|
|
cairo_line_to(cr,
|
|
|
|
-one_over_sqrt_2 * spaceship.radius,
|
|
|
|
one_over_sqrt_2 * spaceship.radius);
|
|
|
|
cairo_line_to(cr, spaceship.radius, 0);
|
|
|
|
cairo_line_to(cr,
|
|
|
|
-one_over_sqrt_2 * spaceship.radius,
|
|
|
|
-one_over_sqrt_2 * spaceship.radius);
|
|
|
|
cairo_line_to(cr, 0, 0);
|
2021-12-11 21:32:48 +01:00
|
|
|
cairo_stroke(cr);
|
2021-12-11 22:29:55 +01:00
|
|
|
cairo_set_source_rgba(cr, 1.0, 0.5, 0.0, 1.0);
|
2021-12-11 21:32:48 +01:00
|
|
|
for (unsigned int i = 0; i < spaceship_thrust; i += 5) {
|
2023-03-14 21:45:20 +01:00
|
|
|
double d_angle = spaceship.radius / (spaceship.radius + 0.1 * i) *
|
|
|
|
0.25 * M_PI * (1 - 0.99 * i / spaceship_thrust);
|
|
|
|
cairo_set_source_rgba(
|
|
|
|
cr, 1.0, 1.0 * (1 - 0.5 * i / spaceship_thrust), 0.0, 1.0);
|
|
|
|
cairo_arc(
|
|
|
|
cr, 0, 0, spaceship.radius + i, M_PI - d_angle, M_PI + d_angle);
|
|
|
|
cairo_stroke(cr);
|
|
|
|
if (d_angle > 0.05)
|
|
|
|
d_angle = 0.7 * d_angle;
|
2021-12-11 21:32:48 +01:00
|
|
|
}
|
|
|
|
cairo_restore(cr);
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:45:20 +01:00
|
|
|
void spaceship_control(double dx, double dy) {
|
|
|
|
spaceship.angle -= dx / 4;
|
2021-12-11 21:32:48 +01:00
|
|
|
if (spaceship.angle < 0)
|
2023-03-14 21:45:20 +01:00
|
|
|
spaceship.angle += 2 * M_PI;
|
|
|
|
else if (spaceship.angle > 2 * M_PI)
|
|
|
|
spaceship.angle -= 2 * M_PI;
|
2021-12-11 21:32:48 +01:00
|
|
|
spaceship_thrust += dy;
|
|
|
|
if (spaceship_thrust > 0)
|
2023-03-14 21:45:20 +01:00
|
|
|
spaceship_thrust_countdown = spaceship_thrust_init;
|
2021-12-11 21:32:48 +01:00
|
|
|
else
|
2023-03-14 21:45:20 +01:00
|
|
|
spaceship_thrust = 0;
|
2021-12-11 21:32:48 +01:00
|
|
|
}
|