Create a ship object.

This commit is contained in:
Nicolás Ortega Froysa 2018-03-25 16:59:03 +02:00
parent a1406c4506
commit 4dc7fbbf04
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF
4 changed files with 148 additions and 1 deletions

View File

@ -32,3 +32,5 @@ extern ALLEGRO_DISPLAY *display; ///< The allegro display.
extern int redraw; ///< Whether or not to redraw the screen. extern int redraw; ///< Whether or not to redraw the screen.
extern int run; ///< Whether or not to continue running the simulation. extern int run; ///< Whether or not to continue running the simulation.
extern int show_help; ///< Whether or not to show the help info.
extern int show_info; ///< Whether or not to show simulation info.

View File

@ -19,9 +19,12 @@
#include "globals.h" #include "globals.h"
#include "display.h" #include "display.h"
#include "event_manager.h" #include "event_manager.h"
#include "ship.h"
int run; int run;
int redraw; int redraw;
int show_help;
int show_info;
#include <stdio.h> #include <stdio.h>
#include <allegro5/allegro.h> #include <allegro5/allegro.h>
@ -53,9 +56,15 @@ int main() {
return 1; return 1;
} }
// initialize the spaceship at the center of the screen
struct ship ship;
ship_init(&ship, 400, 300);
// begin running the simulation // begin running the simulation
run = 1; run = 1;
redraw = 1; redraw = 1;
show_help = 1;
show_info = 1;
while(run) while(run)
{ {
handle_event(); handle_event();
@ -68,9 +77,18 @@ int main() {
* running at a consistent rate, rather than dependent on random * running at a consistent rate, rather than dependent on random
* events. * events.
*/ */
// TODO: run simulation physics ship_update(&ship);
al_clear_to_color(al_map_rgb(0, 0, 0)); al_clear_to_color(al_map_rgb(0, 0, 0));
// TODO: run simulation draw functions // TODO: run simulation draw functions
if(show_info)
{
// TODO: draw simulation stats
}
if(show_help)
{
// TODO: draw help information
}
al_flip_display(); al_flip_display();
redraw = 0; redraw = 0;
} }

81
src/ship.c Normal file
View File

@ -0,0 +1,81 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program 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.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ship.h"
#include "event_manager.h"
#include "globals.h"
#ifdef DEBUG
# include <stdio.h>
#endif
#include <math.h>
#ifndef M_PI
# define M_PI 3.14159265f
#endif
#define ACCEL 5.0f
#define TURN_ACCEL (M_PI / FPS) // turn at pi radians / sec
void ship_init(struct ship *ship, int x, int y) {
if(!ship)
{
#ifdef DEBUG
fprintf(stderr,
"ship_init(struct ship*,int,int): invalid ship.\n");
#endif
return;
}
ship->x = x;
ship->y = y;
ship->velX = ship->velY = 0;
ship->direction = 0;
}
void ship_update(struct ship *ship) {
if(!ship)
{
#ifdef DEBUG
fprintf(stderr,
"ship_update(struct ship*): invalid ship.\n");
#endif
return;
}
if(key_is_down(KEY_UP))
{
ship->velX += cos(ship->direction) * ACCEL;
ship->velY += sin(ship->direction) * ACCEL;
}
if(key_is_down(KEY_DOWN))
{
// moving backwards is slower than moving forward
ship->velX -= cos(ship->direction) * (ACCEL / 2);
ship->velY -= sin(ship->direction) * (ACCEL / 2);
}
if(key_is_down(KEY_RIGHT))
ship->direction -= TURN_ACCEL;
if(key_is_down(KEY_LEFT))
ship->direction += TURN_ACCEL;
// keep direction within bounds
if(ship->direction >= M_PI * 2)
ship->direction -= M_PI * 2;
else if(ship->direction < 0)
ship->direction += M_PI * 2;
}

46
src/ship.h Normal file
View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program 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.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
struct ship {
float x, y; ///< The x and y coordinates of the ship.
float velX, velY; ///< The x and y velocities of the ship.
/**
* The direction that the ship is facing in radians, where
* 0 is right facing.
*/
float direction;
};
/**
* @brief Initialize the ship at a position.
*
* @param ship A pointer to the ship object.
* @param x Initial x position of the ship.
* @param y Initial y position of the ship.
*/
void ship_init(struct ship *ship, int x, int y);
/**
* @brief Updates the ship's variables according to keyboard
* input.
*
* @param ship A pointer to the ship object.
*/
void ship_update(struct ship *ship);