Added ship module, moved fireBullet to it (as ship_fireBullet).

For functions that are used by both aliens and the player.
This commit is contained in:
onpon4 2015-03-13 10:10:58 -04:00
parent 8842d7775b
commit 2a0234c300
9 changed files with 163 additions and 120 deletions

View File

@ -1,7 +1,7 @@
CXXFLAGS ?= -O2 -Wall -g CXXFLAGS ?= -O2 -Wall -g
CXXFLAGS += `pkg-config --cflags sdl2 SDL2_image SDL2_mixer` CXXFLAGS += `pkg-config --cflags sdl2 SDL2_image SDL2_mixer`
LIBS = `pkg-config --libs sdl2 SDL2_image SDL2_mixer` LIBS = `pkg-config --libs sdl2 SDL2_image SDL2_mixer`
OBJS = alien.o audio.o bullet.o cargo.o collectable.o comms.o debris.o events.o explosions.o game.o globals.o graphics.o init.o intermission.o loadSave.o messages.o misc.o missions.o player.o resources.o script.o shop.o Starfighter.o title.o weapons.o OBJS = alien.o audio.o bullet.o cargo.o collectable.o comms.o debris.o events.o explosions.o game.o globals.o graphics.o init.o intermission.o loadSave.o messages.o misc.o missions.o player.o resources.o script.o ship.o shop.o Starfighter.o title.o weapons.o
VERSION = 1.3-dev VERSION = 1.3-dev
PROG = starfighter PROG = starfighter

View File

@ -57,6 +57,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "player.h" #include "player.h"
#include "resources.h" #include "resources.h"
#include "script.h" #include "script.h"
#include "ship.h"
#include "shop.h" #include "shop.h"
#include "title.h" #include "title.h"
#include "weapons.h" #include "weapons.h"

View File

@ -22,6 +22,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
object alien_defs[CD_MAX]; object alien_defs[CD_MAX];
object aliens[ALIEN_MAX]; object aliens[ALIEN_MAX];
/*
This simply pulls back an alien from the array that is
"dead" (no shield) and returns the index number so we can have
a new one.
*/
static int alien_getFreeIndex()
{
for (int i = 0 ; i < engine.maxAliens ; i++)
{
if (!aliens[i].active)
{
return i;
}
}
return -1;
}
void alien_defs_init() void alien_defs_init()
{ {
// Dual Plasma Fighter. // Dual Plasma Fighter.
@ -664,24 +682,6 @@ void alien_defs_init()
alien_defs[CD_URANUSBOSSWING2].flags = FL_WEAPCO | FL_IMMORTAL; alien_defs[CD_URANUSBOSSWING2].flags = FL_WEAPCO | FL_IMMORTAL;
} }
/*
This simply pulls back an alien from the array that is
"dead" (no shield) and returns the index number so we can have
a new one.
*/
static int alien_getFreeIndex()
{
for (int i = 0 ; i < engine.maxAliens ; i++)
{
if (!aliens[i].active)
{
return i;
}
}
return -1;
}
bool alien_add() bool alien_add()
{ {
int index = alien_getFreeIndex(); int index = alien_getFreeIndex();

View File

@ -136,98 +136,6 @@ void bullet_add(object *theWeapon, object *attacker, int y, int dy)
engine.bulletTail = bullet; engine.bulletTail = bullet;
} }
/*
Fill in later...
*/
void fireBullet(object *attacker, int weaponType)
{
if (attacker->reload[weaponType] > 0)
return;
int y = (attacker->image[0]->h) / 5;
// Remove some ammo from the player
if ((attacker == &player) && (weaponType == 1) && (!engine.cheatAmmo))
player.ammo[1]--;
object *theWeapon = &weapon[attacker->weaponType[weaponType]];
switch(theWeapon->id)
{
case WT_PLASMA:
case WT_SPREAD:
case WT_DIRECTIONAL:
audio_playSound(SFX_PLASMA, attacker->x);
break;
case WT_ROCKET:
audio_playSound(SFX_MISSILE, attacker->x);
break;
case WT_LASER:
audio_playSound(SFX_LASER, attacker->x);
break;
case WT_CHARGER:
audio_playSound(SFX_PLASMA3, attacker->x);
break;
}
if (theWeapon->flags & WF_SPREAD && theWeapon->ammo[0] >= 3)
{
bullet_add(theWeapon, attacker, y * 1, -2);
if(theWeapon->ammo[0] != 4)
bullet_add(theWeapon, attacker, y * 3, 0);
if(theWeapon->ammo[0] != 3)
{
bullet_add(theWeapon, attacker, y * 2, -1);
bullet_add(theWeapon, attacker, y * 4, 1);
}
bullet_add(theWeapon, attacker, y * 5, 2);
}
else
{
if(theWeapon->ammo[0] & 1)
bullet_add(theWeapon, attacker, y * 3, 0);
if(theWeapon->ammo[0] >= 2)
{
bullet_add(theWeapon, attacker, y * 2, 0);
bullet_add(theWeapon, attacker, y * 4, 0);
}
if(theWeapon->ammo[0] >= 4)
{
bullet_add(theWeapon, attacker, y * 1, 0);
bullet_add(theWeapon, attacker, y * 5, 0);
}
}
// Reset the weapon reload time. Double it if it is not friendly or
// a boss or Kline
attacker->reload[weaponType] = theWeapon->reload[0];
if ((attacker->flags & FL_WEAPCO) && (attacker != &aliens[ALIEN_BOSS]) &&
(attacker != &aliens[ALIEN_KLINE]) && (theWeapon->id != W_LASER))
attacker->reload[weaponType] *= 2;
if ((engine.cheatAmmo) || (theWeapon->id == WT_LASER))
return;
if ((attacker == &player) && (weaponType == 0))
{
if (player.ammo[0] > 0)
{
player.ammo[0]--;
if (player.ammo[0] <= 0)
{
weapon[W_PLAYER_WEAPON].ammo[0] = currentGame.minPlasmaOutput;
weapon[W_PLAYER_WEAPON].damage = currentGame.minPlasmaDamage;
weapon[W_PLAYER_WEAPON].reload[0] = rate2reload[currentGame.minPlasmaRate];
}
}
}
}
/* /*
Used for homing missiles. When a missile is active and it is told to home in Used for homing missiles. When a missile is active and it is told to home in
on an enemy, it will attempt to randomly grab one every frame if it does not on an enemy, it will attempt to randomly grab one every frame if it does not

View File

@ -21,7 +21,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define BULLETS_H #define BULLETS_H
void bullet_add(object *theWeapon, object *attacker, int y, int dy); void bullet_add(object *theWeapon, object *attacker, int y, int dy);
extern void fireBullet(object *attacker, int weaponType);
extern char checkPlayerShockDamage(float x, float y); extern char checkPlayerShockDamage(float x, float y);
extern void fireRay(object *attacker); extern void fireRay(object *attacker);
extern void doBullets(); extern void doBullets();

View File

@ -756,7 +756,7 @@ int mainGameLoop()
((rand() % 1000 < alien->chance[0]) || ((rand() % 1000 < alien->chance[0]) ||
(alien->flags & FL_CONTINUOUS_FIRE))) (alien->flags & FL_CONTINUOUS_FIRE)))
{ {
fireBullet(alien, 0); ship_fireBullet(alien, 0);
} }
if ((alien->reload[1] == 0) && if ((alien->reload[1] == 0) &&
((rand() % 1000 < alien->chance[1]) || ((rand() % 1000 < alien->chance[1]) ||
@ -767,7 +767,7 @@ int mainGameLoop()
{ {
if (alien->weaponType[1] == W_CHARGER) if (alien->weaponType[1] == W_CHARGER)
alien->ammo[1] = 50 + rand() % 150; alien->ammo[1] = 50 + rand() % 150;
fireBullet(alien, 1); ship_fireBullet(alien, 1);
} }
else if (alien->weaponType[1] == W_LASER) else if (alien->weaponType[1] == W_LASER)
{ {
@ -793,7 +793,7 @@ int mainGameLoop()
if (alien->flags & FL_FIRELASER) if (alien->flags & FL_FIRELASER)
{ {
fireBullet(alien, 1); ship_fireBullet(alien, 1);
if ((rand() % 25) == 0) if ((rand() % 25) == 0)
alien->flags -= FL_FIRELASER; alien->flags -= FL_FIRELASER;
} }

View File

@ -102,21 +102,21 @@ void doPlayer()
if (player.shield > 0) if (player.shield > 0)
{ {
if ((engine.keyState[KEY_FIRE])) if ((engine.keyState[KEY_FIRE]))
fireBullet(&player, 0); ship_fireBullet(&player, 0);
if ((engine.keyState[KEY_ALTFIRE]) && (player.weaponType[1] != W_NONE)) if ((engine.keyState[KEY_ALTFIRE]) && (player.weaponType[1] != W_NONE))
{ {
if ((player.weaponType[1] != W_CHARGER) && if ((player.weaponType[1] != W_CHARGER) &&
(player.weaponType[1] != W_LASER) && (player.ammo[1] > 0)) (player.weaponType[1] != W_LASER) && (player.ammo[1] > 0))
{ {
fireBullet(&player, 1); ship_fireBullet(&player, 1);
} }
if (player.weaponType[1] == W_LASER) if (player.weaponType[1] == W_LASER)
{ {
if (player.ammo[1] < 100) if (player.ammo[1] < 100)
{ {
fireBullet(&player, 1); ship_fireBullet(&player, 1);
player.ammo[1] += 2; player.ammo[1] += 2;
if (player.ammo[1] >= 100) if (player.ammo[1] >= 100)
{ {
@ -136,7 +136,7 @@ void doPlayer()
limitCharAdd(&player.ammo[1], 1, 0, 150); limitCharAdd(&player.ammo[1], 1, 0, 150);
if (player.ammo[1] >= 150) if (player.ammo[1] >= 150)
{ {
fireBullet(&player, 1); ship_fireBullet(&player, 1);
player.ammo[1] = 0; player.ammo[1] = 0;
charger_fired = true; charger_fired = true;
} }
@ -145,7 +145,7 @@ void doPlayer()
else else
{ {
if (player.ammo[1] > 0) if (player.ammo[1] > 0)
fireBullet(&player, 1); ship_fireBullet(&player, 1);
player.ammo[1] = 0; player.ammo[1] = 0;
charger_fired = false; charger_fired = false;
} }

112
src/ship.cpp Normal file
View File

@ -0,0 +1,112 @@
/*
Copyright (C) 2003 Parallel Realities
Copyright (C) 2011, 2012 Guus Sliepen
Copyright (C) 2015 Julian Marchant
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 "Starfighter.h"
/*
Fill in later...
*/
void ship_fireBullet(object *ship, int weaponType)
{
if (ship->reload[weaponType] > 0)
return;
int y = (ship->image[0]->h) / 5;
// Remove some ammo from the player
if ((ship == &player) && (weaponType == 1) && (!engine.cheatAmmo))
player.ammo[1]--;
object *theWeapon = &weapon[ship->weaponType[weaponType]];
switch(theWeapon->id)
{
case WT_PLASMA:
case WT_SPREAD:
case WT_DIRECTIONAL:
audio_playSound(SFX_PLASMA, ship->x);
break;
case WT_ROCKET:
audio_playSound(SFX_MISSILE, ship->x);
break;
case WT_LASER:
audio_playSound(SFX_LASER, ship->x);
break;
case WT_CHARGER:
audio_playSound(SFX_PLASMA3, ship->x);
break;
}
if (theWeapon->flags & WF_SPREAD && theWeapon->ammo[0] >= 3)
{
bullet_add(theWeapon, ship, y * 1, -2);
if(theWeapon->ammo[0] != 4)
bullet_add(theWeapon, ship, y * 3, 0);
if(theWeapon->ammo[0] != 3)
{
bullet_add(theWeapon, ship, y * 2, -1);
bullet_add(theWeapon, ship, y * 4, 1);
}
bullet_add(theWeapon, ship, y * 5, 2);
}
else
{
if(theWeapon->ammo[0] & 1)
bullet_add(theWeapon, ship, y * 3, 0);
if(theWeapon->ammo[0] >= 2)
{
bullet_add(theWeapon, ship, y * 2, 0);
bullet_add(theWeapon, ship, y * 4, 0);
}
if(theWeapon->ammo[0] >= 4)
{
bullet_add(theWeapon, ship, y * 1, 0);
bullet_add(theWeapon, ship, y * 5, 0);
}
}
// Reset the weapon reload time. Double it if it is not friendly or
// a boss or Kline
ship->reload[weaponType] = theWeapon->reload[0];
if ((ship->flags & FL_WEAPCO) && (ship != &aliens[ALIEN_BOSS]) &&
(ship != &aliens[ALIEN_KLINE]) && (theWeapon->id != W_LASER))
ship->reload[weaponType] *= 2;
if ((engine.cheatAmmo) || (theWeapon->id == WT_LASER))
return;
if ((ship == &player) && (weaponType == 0))
{
if (player.ammo[0] > 0)
{
player.ammo[0]--;
if (player.ammo[0] <= 0)
{
weapon[W_PLAYER_WEAPON].ammo[0] = currentGame.minPlasmaOutput;
weapon[W_PLAYER_WEAPON].damage = currentGame.minPlasmaDamage;
weapon[W_PLAYER_WEAPON].reload[0] = rate2reload[currentGame.minPlasmaRate];
}
}
}
}

23
src/ship.h Normal file
View File

@ -0,0 +1,23 @@
/*
Copyright (C) 2015 Julian Marchant
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/>.
*/
#ifndef SHIP_H
#define SHIP_H
void ship_fireBullet(object *ship, int weaponType);
#endif