[Add] Targetting!!!
This commit is contained in:
		
							parent
							
								
									c2ec11a7c3
								
							
						
					
					
						commit
						09f6aea97e
					
				@ -1,7 +1,7 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<Ships>
 | 
			
		||||
	<ship name="Ship">
 | 
			
		||||
		<GFX>ship.png</GFX>
 | 
			
		||||
		<GFX>ship</GFX>
 | 
			
		||||
		<class>1</class>
 | 
			
		||||
		<movement>
 | 
			
		||||
			<thrust>400</thrust>
 | 
			
		||||
@ -27,7 +27,7 @@
 | 
			
		||||
		</outfits>
 | 
			
		||||
  </ship>
 | 
			
		||||
	<ship name="Test">
 | 
			
		||||
		<GFX>ship1.png</GFX>
 | 
			
		||||
		<GFX>ship1</GFX>
 | 
			
		||||
		<class>1</class>
 | 
			
		||||
		<movement>
 | 
			
		||||
			<thrust>180</thrust>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								gfx/gui/pilot.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								gfx/gui/pilot.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 181 B  | 
							
								
								
									
										
											BIN
										
									
								
								gfx/gui/planet.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								gfx/gui/planet.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 326 B  | 
							
								
								
									
										
											BIN
										
									
								
								gfx/ship/ship1_target.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								gfx/ship/ship1_target.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 12 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								gfx/ship/ship1_target.xcf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								gfx/ship/ship1_target.xcf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								gfx/ship/ship_target.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								gfx/ship/ship_target.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 12 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								gfx/ship/ship_target.xcf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								gfx/ship/ship_target.xcf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							@ -26,6 +26,8 @@
 | 
			
		||||
#define CONF_FILE       "conf"
 | 
			
		||||
#define MINIMUM_FPS     0.5
 | 
			
		||||
 | 
			
		||||
#define FONT_SIZE       10
 | 
			
		||||
 | 
			
		||||
extern const char* keybindNames[]; // Keybindings.
 | 
			
		||||
 | 
			
		||||
static int quit = 0; // Primary loop.
 | 
			
		||||
@ -80,6 +82,7 @@ int main(int argc, char** argv) {
 | 
			
		||||
  input_setKeybind("left",        KEYBIND_KEYBOARD, SDLK_a,     0);
 | 
			
		||||
  input_setKeybind("right",       KEYBIND_KEYBOARD, SDLK_d,     0);
 | 
			
		||||
  input_setKeybind("primary",     KEYBIND_KEYBOARD, SDLK_SPACE, 0);
 | 
			
		||||
  input_setKeybind("target",      KEYBIND_KEYBOARD, SDLK_TAB,   0);
 | 
			
		||||
  input_setKeybind("mapzoomin",   KEYBIND_KEYBOARD, SDLK_UP,    0);
 | 
			
		||||
  input_setKeybind("mapzoomout",  KEYBIND_KEYBOARD, SDLK_DOWN,  0);
 | 
			
		||||
 | 
			
		||||
@ -237,7 +240,7 @@ int main(int argc, char** argv) {
 | 
			
		||||
  if(ai_init())
 | 
			
		||||
    WARN("Error initializing AI");
 | 
			
		||||
 | 
			
		||||
  gl_fontInit(NULL, NULL, 16);
 | 
			
		||||
  gl_fontInit(NULL, NULL, FONT_SIZE); // Init default font size.
 | 
			
		||||
  gui_init(); // Init the GUI crap.
 | 
			
		||||
 | 
			
		||||
  // Data loading.
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								src/pilot.c
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/pilot.c
									
									
									
									
									
								
							@ -25,6 +25,17 @@ static void pilot_update(Pilot* pilot, const double dt);
 | 
			
		||||
void pilot_render(Pilot* pilot);
 | 
			
		||||
static void pilot_free(Pilot* p);
 | 
			
		||||
 | 
			
		||||
// Get the next pilot based on player_id.
 | 
			
		||||
unsigned int pilot_getNext(unsigned int id) {
 | 
			
		||||
  int i, n;
 | 
			
		||||
  for(i = 0, n = pilots/2; n > 0; n /= 2)
 | 
			
		||||
    i += (pilot_stack[i+n]->id > id) ? 0 : n;
 | 
			
		||||
 | 
			
		||||
  if(i == pilots-1) return 0;
 | 
			
		||||
 | 
			
		||||
  return pilot_stack[i+1]->id;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Pull a pilot out of the pilot_stack based on id.
 | 
			
		||||
Pilot* get_pilot(unsigned int id) {
 | 
			
		||||
  // Regular search.
 | 
			
		||||
 | 
			
		||||
@ -7,6 +7,7 @@
 | 
			
		||||
 | 
			
		||||
// Aproximation for pilot size.
 | 
			
		||||
#define PILOT_SIZE_APROX  0.8
 | 
			
		||||
#define PILOT_DISABLED    0.2 // Based on armor percentage.
 | 
			
		||||
 | 
			
		||||
// Creation flags.
 | 
			
		||||
#define PILOT_PLAYER    1 // Pilot is a player.
 | 
			
		||||
@ -46,6 +47,7 @@ typedef struct Pilot {
 | 
			
		||||
// Grabing pilot crap.
 | 
			
		||||
extern Pilot* player; // The player.
 | 
			
		||||
Pilot* get_pilot(unsigned int id);
 | 
			
		||||
unsigned int pilot_getNext(unsigned int id);
 | 
			
		||||
 | 
			
		||||
// MISC.
 | 
			
		||||
void pilot_shoot(Pilot* p, int secondary);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										75
									
								
								src/player.c
									
									
									
									
									
								
							
							
						
						
									
										75
									
								
								src/player.c
									
									
									
									
									
								
							@ -7,6 +7,10 @@
 | 
			
		||||
 | 
			
		||||
#define POW2(x) ((x)*(x))
 | 
			
		||||
 | 
			
		||||
#define GFX_GUI_FRAME        "../gfx/gui/frame.png"
 | 
			
		||||
#define GFX_GUI_TARG_PILOT   "../gfx/gui/pilot.png"
 | 
			
		||||
#define GFX_GUI_TARG_PLANET  "../gfx/gui/planet.png"
 | 
			
		||||
 | 
			
		||||
#define KEY_PRESS    1.
 | 
			
		||||
#define KEY_RELEASE -1.
 | 
			
		||||
 | 
			
		||||
@ -19,7 +23,7 @@ typedef struct {
 | 
			
		||||
} Keybind;
 | 
			
		||||
static Keybind** player_input; // Contains the players keybindings.
 | 
			
		||||
// Name of each keybinding.
 | 
			
		||||
const char* keybindNames[] = { "accel", "left", "right", "primary",
 | 
			
		||||
const char* keybindNames[] = { "accel", "left", "right", "primary", "target",
 | 
			
		||||
      "mapzoomin", "mapzoomout" };
 | 
			
		||||
 | 
			
		||||
// Player stuff.
 | 
			
		||||
@ -27,6 +31,7 @@ Pilot* player = NULL; // extern in pilot.h
 | 
			
		||||
static double player_turn = 0.; // Turn velocity from input.
 | 
			
		||||
static double player_acc  = 0.; // Accel velocity from input.
 | 
			
		||||
static int player_primary = 0; // Player is shooting primary weapon.
 | 
			
		||||
static unsigned int player_target = 0; // Targetted pilot.
 | 
			
		||||
 | 
			
		||||
// Pilot stuff for GUI.
 | 
			
		||||
extern Pilot** pilot_stack;
 | 
			
		||||
@ -62,6 +67,7 @@ typedef struct {
 | 
			
		||||
#define RADAR_RES_MAX       100.
 | 
			
		||||
#define RADAR_RES_MIN       10.
 | 
			
		||||
#define RADAR_RES_INTERVAL  10.
 | 
			
		||||
#define RADAR_RES_DEFAULT   40.
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
  double w,h;
 | 
			
		||||
@ -70,12 +76,14 @@ typedef struct {
 | 
			
		||||
typedef struct {
 | 
			
		||||
  // graphics.
 | 
			
		||||
  gl_texture* gfx_frame;
 | 
			
		||||
  gl_texture* gfx_targetPilot, *gfx_targetPlanet;
 | 
			
		||||
  Radar radar;
 | 
			
		||||
  Rect shield, armor, energy;
 | 
			
		||||
  // Positions.
 | 
			
		||||
  Vec2 pos_frame;
 | 
			
		||||
  Vec2 pos_radar;
 | 
			
		||||
  Vec2 pos_shield, pos_armor, pos_energy;
 | 
			
		||||
  Vec2 pos_target, pos_target_health;
 | 
			
		||||
} GUI;
 | 
			
		||||
 | 
			
		||||
GUI gui; // Le Gui!
 | 
			
		||||
@ -87,6 +95,26 @@ extern void pilot_render(Pilot* pilot); // Extern is in Pilot.*
 | 
			
		||||
 | 
			
		||||
// Render the player.
 | 
			
		||||
void player_render(void) {
 | 
			
		||||
  int i;
 | 
			
		||||
  double x, y, sx, sy;
 | 
			
		||||
  Pilot* p;
 | 
			
		||||
  Vec2 v;
 | 
			
		||||
 | 
			
		||||
  if(player_target) {
 | 
			
		||||
    p = get_pilot(player_target);
 | 
			
		||||
 | 
			
		||||
    vect_csetmin(&v, VX(p->solid->pos) - p->ship->gfx_space->sw * PILOT_SIZE_APROX/2.,
 | 
			
		||||
          VY(p->solid->pos) + p->ship->gfx_space->sh * PILOT_SIZE_APROX/2.);
 | 
			
		||||
    gl_blitSprite(gui.gfx_targetPilot, &v, 0, 0);
 | 
			
		||||
    VX(v) += p->ship->gfx_space->sw * PILOT_SIZE_APROX;
 | 
			
		||||
    gl_blitSprite(gui.gfx_targetPilot, &v, 1, 0);
 | 
			
		||||
    VY(v) -= p->ship->gfx_space->sh * PILOT_SIZE_APROX;
 | 
			
		||||
    gl_blitSprite(gui.gfx_targetPilot, &v, 1, 1);
 | 
			
		||||
    VX(v) -= p->ship->gfx_space->sw * PILOT_SIZE_APROX;
 | 
			
		||||
    gl_blitSprite(gui.gfx_targetPilot, &v, 0, 1);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Render the player.
 | 
			
		||||
  pilot_render(player);
 | 
			
		||||
 | 
			
		||||
  // GUI!
 | 
			
		||||
@ -111,9 +139,6 @@ void player_render(void) {
 | 
			
		||||
    glVertex2d( -1.,  0. );
 | 
			
		||||
    glVertex2d( -2.,  0. );
 | 
			
		||||
 | 
			
		||||
  int i;
 | 
			
		||||
  double x, y, sx, sy;
 | 
			
		||||
  Pilot* p;
 | 
			
		||||
  switch(gui.radar.shape) {
 | 
			
		||||
    case RADAR_RECT:
 | 
			
		||||
      glColor4d(COLOR(cRadar_weap));
 | 
			
		||||
@ -188,19 +213,36 @@ void player_render(void) {
 | 
			
		||||
    glVertex2d(x+sx, y-sy);
 | 
			
		||||
    glVertex2d(x, y-sy);
 | 
			
		||||
  glEnd();
 | 
			
		||||
 | 
			
		||||
  // Target.
 | 
			
		||||
  if(player_target) {
 | 
			
		||||
    gl_blitStatic(p->ship->gfx_target, &gui.pos_target);
 | 
			
		||||
    if(p->armor < p->armor_max * PILOT_DISABLED)
 | 
			
		||||
      // Disable the pilot.
 | 
			
		||||
      gl_print(NULL, &gui.pos_target_health, "Disabled");
 | 
			
		||||
    else if(p->shield > p->shield_max / 100.)
 | 
			
		||||
      // On shields.
 | 
			
		||||
      gl_print(NULL, &gui.pos_target_health, "%s: %.0f%%", "Shield", p->shield/p->shield_max*100.);
 | 
			
		||||
    else
 | 
			
		||||
      // On armor.
 | 
			
		||||
      gl_print(NULL, &gui.pos_target_health, "%s: %.0f%%", "Armor", p->armor/p->armor_max*100.);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Init GUI.
 | 
			
		||||
int gui_init(void) {
 | 
			
		||||
// -- Frame.
 | 
			
		||||
  gui.gfx_frame = gl_newImage("../gfx/gui/frame.png");
 | 
			
		||||
  // -- Targeting.
 | 
			
		||||
  gui.gfx_targetPilot = gl_newSprite(GFX_GUI_TARG_PILOT, 2, 2);
 | 
			
		||||
  gui.gfx_targetPlanet = gl_newSprite(GFX_GUI_TARG_PLANET, 2, 2);
 | 
			
		||||
  // -- Frame.
 | 
			
		||||
  gui.gfx_frame = gl_newImage(GFX_GUI_FRAME);
 | 
			
		||||
  vect_csetmin(&gui.pos_frame,
 | 
			
		||||
        gl_screen.w - gui.gfx_frame->w,   // x.
 | 
			
		||||
        gl_screen.h - gui.gfx_frame->h);  // y.
 | 
			
		||||
  gui_xoff = -gui.gfx_frame->w/2.; // Offset is only horizontal and on the right side.
 | 
			
		||||
 | 
			
		||||
// -- Radar.
 | 
			
		||||
  gui.radar.res = 10.;
 | 
			
		||||
  // -- Radar.
 | 
			
		||||
  gui.radar.res = RADAR_RES_DEFAULT;
 | 
			
		||||
  gui.radar.w   = 128.;
 | 
			
		||||
  gui.radar.h   = 128.;
 | 
			
		||||
  gui.radar.shape = RADAR_RECT; //RADAR_CIRCLE;
 | 
			
		||||
@ -208,7 +250,7 @@ int gui_init(void) {
 | 
			
		||||
        VX(gui.pos_frame) + 11,                       // x
 | 
			
		||||
        VY(gui.pos_frame) + gui.gfx_frame->h - 10);   // y.
 | 
			
		||||
 | 
			
		||||
// -- Bars.
 | 
			
		||||
  // -- Bars.
 | 
			
		||||
  gui.shield.w = gui.armor.w = gui.energy.w = 128;
 | 
			
		||||
  gui.shield.h = gui.armor.h = gui.energy.h = 10;
 | 
			
		||||
  vect_csetmin(&gui.pos_shield,
 | 
			
		||||
@ -223,6 +265,16 @@ int gui_init(void) {
 | 
			
		||||
        VX(gui.pos_frame) + 10,                       // x
 | 
			
		||||
        VY(gui.pos_frame) + gui.gfx_frame->h - 236);  // y.
 | 
			
		||||
 | 
			
		||||
  // Target.
 | 
			
		||||
  vect_csetmin(&gui.pos_target,
 | 
			
		||||
        VX(gui.pos_frame) + 10,
 | 
			
		||||
        VY(gui.pos_frame) + gui.gfx_frame->h - 256 - SHIP_TARGET_H);
 | 
			
		||||
 | 
			
		||||
  vect_csetmin(&gui.pos_target_health,
 | 
			
		||||
        VX(gui.pos_frame) + 10 + 10,
 | 
			
		||||
        VY(gui.pos_frame) + gui.gfx_frame->h - 256 - SHIP_TARGET_H + 10);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -305,9 +357,12 @@ static void input_key(int keynum, double value, int abs) {
 | 
			
		||||
  }
 | 
			
		||||
  // Shoot primary weapon. BOOM BOOM.
 | 
			
		||||
  else if(strcmp(player_input[keynum]->name, "primary")==0) {
 | 
			
		||||
    if(value==KEY_PRESS) player_primary = 1;
 | 
			
		||||
    if(value == KEY_PRESS) player_primary = 1;
 | 
			
		||||
    else if(value == KEY_RELEASE) player_primary = 0;
 | 
			
		||||
  }
 | 
			
		||||
  else if(strcmp(player_input[keynum]->name, "target")==0) {
 | 
			
		||||
    if(value == KEY_PRESS) player_target = pilot_getNext(player_target);
 | 
			
		||||
  }
 | 
			
		||||
  else if(strcmp(player_input[keynum]->name, "mapzoomin")==0) {
 | 
			
		||||
    if(value == KEY_PRESS && gui.radar.res < RADAR_RES_MAX)
 | 
			
		||||
      gui.radar.res += RADAR_RES_INTERVAL;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										12
									
								
								src/ship.c
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/ship.c
									
									
									
									
									
								
							@ -14,6 +14,8 @@
 | 
			
		||||
 | 
			
		||||
#define SHIP_DATA   "../dat/ship.xml"
 | 
			
		||||
#define SHIP_GFX    "../gfx/ship/"
 | 
			
		||||
#define SHIP_EXT    ".png"
 | 
			
		||||
#define SHIP_TARGET "_target"
 | 
			
		||||
 | 
			
		||||
static Ship* ship_stack = NULL;
 | 
			
		||||
static int ships = 0;
 | 
			
		||||
@ -47,9 +49,14 @@ static Ship* ship_parse(xmlNodePtr parent) {
 | 
			
		||||
 | 
			
		||||
  while((node = node->next)) { // Load all the data.
 | 
			
		||||
    if(strcmp((char*)node->name, "GFX")==0) {
 | 
			
		||||
      snprintf(str, strlen((char*)node->children->content)+sizeof(SHIP_GFX),
 | 
			
		||||
            SHIP_GFX"%s", (char*)node->children->content);
 | 
			
		||||
      snprintf(str, strlen((char*)node->children->content)+sizeof(SHIP_GFX)+sizeof(SHIP_EXT),
 | 
			
		||||
            SHIP_GFX"%s"SHIP_EXT, (char*)node->children->content);
 | 
			
		||||
      tmp->gfx_space = gl_newSprite(str, 6, 6);
 | 
			
		||||
      // Target.
 | 
			
		||||
      snprintf(str, strlen((char*)node->children->content)+sizeof(SHIP_GFX)+sizeof(SHIP_TARGET)+sizeof(SHIP_EXT),
 | 
			
		||||
            SHIP_GFX"%s"SHIP_TARGET SHIP_EXT, (char*)node->children->content);
 | 
			
		||||
      tmp->gfx_target = gl_newImage(str);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
    else if(strcmp((char*)node->name, "class")==0)
 | 
			
		||||
      tmp->class = atoi((char*)node->children->content);
 | 
			
		||||
@ -192,6 +199,7 @@ void ships_free(void) {
 | 
			
		||||
      free(sot);
 | 
			
		||||
    }
 | 
			
		||||
    gl_freeTexture((ship_stack+i)->gfx_space);
 | 
			
		||||
    gl_freeTexture((ship_stack+i)->gfx_target);
 | 
			
		||||
  }
 | 
			
		||||
  free(ship_stack);
 | 
			
		||||
  ship_stack = NULL;
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,10 @@
 | 
			
		||||
#include "outfit.h"
 | 
			
		||||
#include "opengl.h"
 | 
			
		||||
 | 
			
		||||
// Target gfx dimensions.
 | 
			
		||||
#define SHIP_TARGET_W 128
 | 
			
		||||
#define SHIP_TARGET_H 96
 | 
			
		||||
 | 
			
		||||
enum ship_class { 
 | 
			
		||||
  SHIP_CLASS_NULL,
 | 
			
		||||
  SHIP_CLASS_CIV_LIGHT,
 | 
			
		||||
 | 
			
		||||
@ -154,7 +154,7 @@ static Planet* planet_get(const char* name) {
 | 
			
		||||
            if(strcmp((char*)cur->name, "text")==0) {
 | 
			
		||||
              snprintf(str, strlen((char*)cur->content)+sizeof(PLANET_GFX),
 | 
			
		||||
                    PLANET_GFX"%s", (char*)cur->content);
 | 
			
		||||
              tmp->gfx_space = gl_newSprite(str, 1, 1);
 | 
			
		||||
              tmp->gfx_space = gl_newImage(str);
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          else if(strcmp((char*)node->name, "pos")==0) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user