Skip to content

Commit

Permalink
Shop ship fire when accelerating.
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Jan 7, 2023
1 parent b2ee8e3 commit 06d479e
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions app.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@
#include <gui/scene_manager.h>
#include <math.h>

#ifndef PI
#define PI 3.14159265358979f
#endif

#define TAG "Asteroids" // Used for logging
#define DEBUG_MSG 1
#define SCREEN_XRES 128
#define SCREEN_YRES 64
#define GAME_START_LIVES 3
#ifndef PI
#define PI 3.14159265358979f
#endif

/* The game uses the OK button both to fire and to accelerate the ship.
* This makes it a lot more playable since the finger does not have to
* move between two keys. However it is important that the extra time the
* player needs to press the button to accelerate instead of just firing
* is precisely selected to provide a smooth experience. After a few
* attempts, it looks like 70 milliesconds is the right spot. */
#define SHIP_ACCELERATION_KEYPRESS_TIME 70

/* ============================ Data structures ============================= */

Expand Down Expand Up @@ -107,6 +116,12 @@ Poly ShipPoly = {
3
};

Poly ShipFirePoly = {
{-1.5, 0, 1.5},
{-3, -6, -3},
3
};

/* Rotate the point of the poligon 'poly' and store the new rotated
* polygon in 'rot'. The polygon is rotated by an angle 'a', with
* center at 0,0. */
Expand Down Expand Up @@ -236,6 +251,8 @@ void render_callback(Canvas *const canvas, void *ctx) {

/* Draw ship, asteroids, bullets. */
draw_poly(canvas,&ShipPoly,app->ship.x,app->ship.y,app->ship.rot);
if (key_pressed_time(app,InputKeyOk) > SHIP_ACCELERATION_KEYPRESS_TIME)
draw_poly(canvas,&ShipFirePoly,app->ship.x,app->ship.y,app->ship.rot);

for (int j = 0; j < app->bullets_num; j++)
draw_bullet(canvas,&app->bullets[j]);
Expand Down Expand Up @@ -428,7 +445,7 @@ void restart_game_after_gameover(AsteroidsApp *app) {
app->ticks = 0;
app->score = 0;
app->ship_hit = 0;
app->lives = GAME_START_LIVES;
app->lives = GAME_START_LIVES-1; /* -1 to account for current one. */
restart_game(app);
}

Expand Down Expand Up @@ -526,7 +543,7 @@ void game_tick(void *ctx) {
/* Handle keypresses. */
if (app->pressed[InputKeyLeft]) app->ship.rot -= .35;
if (app->pressed[InputKeyRight]) app->ship.rot += .35;
if (key_pressed_time(app,InputKeyOk) > 70) {
if (key_pressed_time(app,InputKeyOk) > SHIP_ACCELERATION_KEYPRESS_TIME) {
app->ship.vx -= 0.5*(float)sin(app->ship.rot);
app->ship.vy += 0.5*(float)cos(app->ship.rot);
} else if (app->pressed[InputKeyDown]) {
Expand Down

0 comments on commit 06d479e

Please sign in to comment.