83 lines
2.6 KiB
C++
83 lines
2.6 KiB
C++
#include "balls.h"
|
|
#include "game.h"
|
|
|
|
#include <gtk/gtk.h>
|
|
#include <math.h>
|
|
|
|
struct ball spaceship;
|
|
double spaceship_thrust = 0;
|
|
int spaceship_thrust_countdown = 0;
|
|
int spaceship_thrust_init = 50;
|
|
|
|
void spaceship_init_state() {
|
|
spaceship.position.x = width / 2;
|
|
spaceship.position.y = height / 2;
|
|
spaceship.radius = 30;
|
|
spaceship.velocity.x = 0;
|
|
spaceship.velocity.y = 0;
|
|
spaceship.angle = 0;
|
|
spaceship.v_angle = 0;
|
|
}
|
|
|
|
void spaceship_update_state() {
|
|
if (spaceship_thrust > 0) {
|
|
vec2d f{cos(spaceship.angle) * spaceship_thrust * 4.0,
|
|
sin(spaceship.angle) * spaceship_thrust * 4.0};
|
|
|
|
spaceship.position += delta * delta * f / 2.0;
|
|
spaceship.velocity += delta * f;
|
|
if (spaceship_thrust_countdown > 0)
|
|
--spaceship_thrust_countdown;
|
|
else
|
|
spaceship_thrust = 0;
|
|
}
|
|
ball_update_state(&spaceship);
|
|
}
|
|
|
|
void spaceship_draw(cairo_t* cr) {
|
|
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);
|
|
cairo_translate(cr, spaceship.position.x, spaceship.position.y);
|
|
cairo_rotate(cr, spaceship.angle);
|
|
cairo_arc(cr, 0, 0, spaceship.radius, 0, 2 * M_PI);
|
|
cairo_stroke(cr);
|
|
cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);
|
|
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);
|
|
cairo_stroke(cr);
|
|
cairo_set_source_rgba(cr, 1.0, 0.5, 0.0, 1.0);
|
|
for (unsigned int i = 0; i < spaceship_thrust; i += 5) {
|
|
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;
|
|
}
|
|
cairo_restore(cr);
|
|
}
|
|
|
|
void spaceship_control(double dx, double dy) {
|
|
spaceship.angle -= dx / 4;
|
|
if (spaceship.angle < 0)
|
|
spaceship.angle += 2 * M_PI;
|
|
else if (spaceship.angle > 2 * M_PI)
|
|
spaceship.angle -= 2 * M_PI;
|
|
spaceship_thrust += dy;
|
|
if (spaceship_thrust > 0)
|
|
spaceship_thrust_countdown = spaceship_thrust_init;
|
|
else
|
|
spaceship_thrust = 0;
|
|
}
|