From 2a0234c3000d5ebb07521aabc6269269c89a21f3 Mon Sep 17 00:00:00 2001 From: onpon4 Date: Fri, 13 Mar 2015 10:10:58 -0400 Subject: [PATCH] Added ship module, moved fireBullet to it (as ship_fireBullet). For functions that are used by both aliens and the player. --- Makefile | 2 +- src/Starfighter.h | 1 + src/alien.cpp | 36 +++++++-------- src/bullet.cpp | 92 ------------------------------------- src/bullet.h | 1 - src/game.cpp | 6 +-- src/player.cpp | 10 ++--- src/ship.cpp | 112 ++++++++++++++++++++++++++++++++++++++++++++++ src/ship.h | 23 ++++++++++ 9 files changed, 163 insertions(+), 120 deletions(-) create mode 100644 src/ship.cpp create mode 100644 src/ship.h diff --git a/Makefile b/Makefile index 98fb631..2326a2b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CXXFLAGS ?= -O2 -Wall -g CXXFLAGS += `pkg-config --cflags 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 PROG = starfighter diff --git a/src/Starfighter.h b/src/Starfighter.h index 0ec2187..6ffade6 100644 --- a/src/Starfighter.h +++ b/src/Starfighter.h @@ -57,6 +57,7 @@ along with this program. If not, see . #include "player.h" #include "resources.h" #include "script.h" +#include "ship.h" #include "shop.h" #include "title.h" #include "weapons.h" diff --git a/src/alien.cpp b/src/alien.cpp index a41d8ba..c7914ac 100644 --- a/src/alien.cpp +++ b/src/alien.cpp @@ -22,6 +22,24 @@ along with this program. If not, see . object alien_defs[CD_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() { // Dual Plasma Fighter. @@ -664,24 +682,6 @@ void alien_defs_init() 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() { int index = alien_getFreeIndex(); diff --git a/src/bullet.cpp b/src/bullet.cpp index 8849c60..aa47b4f 100644 --- a/src/bullet.cpp +++ b/src/bullet.cpp @@ -136,98 +136,6 @@ void bullet_add(object *theWeapon, object *attacker, int y, int dy) 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 on an enemy, it will attempt to randomly grab one every frame if it does not diff --git a/src/bullet.h b/src/bullet.h index 1a634d6..22be9a8 100644 --- a/src/bullet.h +++ b/src/bullet.h @@ -21,7 +21,6 @@ along with this program. If not, see . #define BULLETS_H 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 void fireRay(object *attacker); extern void doBullets(); diff --git a/src/game.cpp b/src/game.cpp index 0bfdfe2..5dc0bca 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -756,7 +756,7 @@ int mainGameLoop() ((rand() % 1000 < alien->chance[0]) || (alien->flags & FL_CONTINUOUS_FIRE))) { - fireBullet(alien, 0); + ship_fireBullet(alien, 0); } if ((alien->reload[1] == 0) && ((rand() % 1000 < alien->chance[1]) || @@ -767,7 +767,7 @@ int mainGameLoop() { if (alien->weaponType[1] == W_CHARGER) alien->ammo[1] = 50 + rand() % 150; - fireBullet(alien, 1); + ship_fireBullet(alien, 1); } else if (alien->weaponType[1] == W_LASER) { @@ -793,7 +793,7 @@ int mainGameLoop() if (alien->flags & FL_FIRELASER) { - fireBullet(alien, 1); + ship_fireBullet(alien, 1); if ((rand() % 25) == 0) alien->flags -= FL_FIRELASER; } diff --git a/src/player.cpp b/src/player.cpp index a7d7c7e..c737073 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -102,21 +102,21 @@ void doPlayer() if (player.shield > 0) { if ((engine.keyState[KEY_FIRE])) - fireBullet(&player, 0); + ship_fireBullet(&player, 0); if ((engine.keyState[KEY_ALTFIRE]) && (player.weaponType[1] != W_NONE)) { if ((player.weaponType[1] != W_CHARGER) && (player.weaponType[1] != W_LASER) && (player.ammo[1] > 0)) { - fireBullet(&player, 1); + ship_fireBullet(&player, 1); } if (player.weaponType[1] == W_LASER) { if (player.ammo[1] < 100) { - fireBullet(&player, 1); + ship_fireBullet(&player, 1); player.ammo[1] += 2; if (player.ammo[1] >= 100) { @@ -136,7 +136,7 @@ void doPlayer() limitCharAdd(&player.ammo[1], 1, 0, 150); if (player.ammo[1] >= 150) { - fireBullet(&player, 1); + ship_fireBullet(&player, 1); player.ammo[1] = 0; charger_fired = true; } @@ -145,7 +145,7 @@ void doPlayer() else { if (player.ammo[1] > 0) - fireBullet(&player, 1); + ship_fireBullet(&player, 1); player.ammo[1] = 0; charger_fired = false; } diff --git a/src/ship.cpp b/src/ship.cpp new file mode 100644 index 0000000..027c603 --- /dev/null +++ b/src/ship.cpp @@ -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 . +*/ + +#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]; + } + } + } +} diff --git a/src/ship.h b/src/ship.h new file mode 100644 index 0000000..a310724 --- /dev/null +++ b/src/ship.h @@ -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 . +*/ + +#ifndef SHIP_H +#define SHIP_H + +void ship_fireBullet(object *ship, int weaponType); + +#endif