/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx 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 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Knecht co-programmer: Silvan Nellen */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY #include "executor/executor.h" #include "space_ship.h" #include "util/loading/resource_manager.h" #include "weapons/test_gun.h" #include "weapons/light_blaster.h" #include "weapons/medium_blaster.h" #include "weapons/heavy_blaster.h" #include "weapons/swarm_launcher.h" #include "weapons/spike_launcher.h" #include "weapons/spike_thrower.h" #include "weapons/boomerang_gun.h" #include "weapons/turret.h" #include "weapons/cannon.h" #include "elements/glgui_energywidgetvertical.h" #include "glgui_bar.h" #include "particles/dot_emitter.h" #include "particles/emitter_node.h" #include "particles/sprite_particles.h" #include "effects/trail.h" #include "effects/wobblegrid.h" #include "util/loading/factory.h" #include "key_mapper.h" #include "network_game_manager.h" #include "shared_network_data.h" #include "power_ups/weapon_power_up.h" #include "power_ups/param_power_up.h" #include "graphics_engine.h" #include "plane.h" #include "state.h" #include "player.h" #include "util/loading/load_param.h" #include "time.h" #include "track/track.h" // #include "lib/gui/gl_gui/glgui_bar.h" // #include "lib/gui/gl_gui/glgui_pushbutton.h" #include "class_id_DEPRECATED.h" ObjectListDefinitionID(SpaceShip, CL_SPACE_SHIP); CREATE_FACTORY(SpaceShip); #include "script_class.h" CREATE_SCRIPTABLE_CLASS(SpaceShip, addMethod("hasPlayer", Executor0ret(&Playable::hasPlayer)) ->addMethod("fire", Executor1(&Playable::fire)) ->addMethod("loadModel", Executor2(&WorldEntity::loadModel2)) ->addMethod("setName", Executor1(&BaseObject::setName)) ->addMethod("hide", Executor0(&WorldEntity::hide)) ->addMethod("unhide", Executor0(&WorldEntity::unhide)) //Coordinates ->addMethod("setAbsCoor", Executor3(&PNode::setAbsCoor)) ->addMethod("getAbsCoorX", Executor0ret(&PNode::getAbsCoorX)) ->addMethod("getAbsCoorY", Executor0ret(&PNode::getAbsCoorY)) ->addMethod("getAbsCoorZ", Executor0ret(&PNode::getAbsCoorZ)) //->addMethod("setCameraSpeed", Executor1(&SpaceShip::setCameraSpeed)) ); /** * destructs the spaceship, deletes alocated memory */ SpaceShip::~SpaceShip () { this->setPlayer(NULL); } /** * loads a Spaceships information from a specified file. * @param fileName the name of the File to load the spaceship from (absolute path) */ SpaceShip::SpaceShip(const std::string& fileName) : secWeaponMan(this) //, //supportedPlaymodes(Playable::Vertical) , //playmode(Playable::Vertical) { this->init(); TiXmlDocument doc(fileName); if(!doc.LoadFile()) { PRINTF(2)("Loading file %s failed for spaceship.\n", fileName.c_str()); return; } this->loadParams(doc.RootElement()); } /** * creates a new Spaceship from Xml Data * @param root the xml element containing spaceship data @todo add more parameters to load */ SpaceShip::SpaceShip(const TiXmlElement* root) : secWeaponMan(this) //, //supportedPlaymodes(Playable::Vertical) , //playmode(Playable::Vertical) { this->init(); //this->setParentMode(PNODE_REPARENT_DELETE_CHILDREN); if (root != NULL) this->loadParams(root); } /** * initializes a Spaceship */ void SpaceShip::init() { srand(time(0)); //initialize Random Nomber Generator // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); this->registerObject(this, SpaceShip::_objectList); PRINTF(4)("SPACESHIP INIT\n"); this->weaponMan.setParentEntity( this); //weapons: Weapon* wpRight1 = new LightBlaster (); wpRight1->setName( "LightBlaster"); //wpRight1->setParent( this); Weapon* wpLeft1 = new LightBlaster (); wpLeft1->setName( "LightBlaster"); //wpLeft1->setParent( this); Weapon* wpRight2 = new MediumBlaster (); wpRight2->setName( "MediumBlaster"); //wpRight2->setParent( this); Weapon* wpLeft2 = new MediumBlaster (); wpLeft2->setName( "MediumBlaster"); //wpLeft2->setParent( this); Weapon* wpRight3 = new HeavyBlaster (1); wpRight3->setName( "HeavyBlaster"); //wpRight3->setParent( this); Weapon* wpLeft3 = new HeavyBlaster (0); wpLeft3->setName( "HeavyBlaster"); //wpLeft3->setParent( this); Weapon* cannon = new SwarmLauncher(); cannon->setName( "SwarmLauncher"); Weapon* spike = new SpikeThrower(); spike->setName( "SpikeThrower" ); Weapon* spike2 = new SpikeLauncher(); spike->setName( "SpikeLauncher" ); this->weaponMan.addWeapon( wpLeft1, 0, 0); this->weaponMan.addWeapon( wpRight1, 0, 1); this->weaponMan.addWeapon( wpLeft2, 1, 2); this->weaponMan.addWeapon( wpRight2, 1, 3); this->weaponMan.addWeapon( wpLeft3, 2, 4); this->weaponMan.addWeapon( wpRight3, 2, 5); this->weaponMan.addWeapon( wpLeft1, 3, 0); this->weaponMan.addWeapon( wpRight1, 3, 1); this->weaponMan.addWeapon( wpLeft2, 3, 2); this->weaponMan.addWeapon( wpRight2, 3, 3); this->weaponMan.addWeapon( wpLeft3, 3, 4); this->weaponMan.addWeapon( wpRight3, 3, 5); this->secWeaponMan.addWeapon( cannon, 0, 0); this->secWeaponMan.addWeapon( spike, 1, 1); this->secWeaponMan.addWeapon( spike2, 2, 2); this->weaponMan.changeWeaponConfig(3); this->secWeaponMan.changeWeaponConfig(1); curWeaponPrimary = 3; curWeaponSecondary = 2; Playable::weaponConfigChanged(); reactorOutput = 10; weaponEnergyRegen = 10; // 10 einheiten pro Sekunde engineSpeedBase = 5; shieldRegen = 2; shieldEnergyShare = 0.3; weaponEnergyShare = 0.3; engineEnergyShare = 0.4; shieldCur = 20; shieldMax = 100; shieldTH = .2* shieldMax; // shield power must be 20% before shield kicks in again this->setHealth( 20); this->setHealthMax( 100); electronicCur = 50; electronicMax = 50; electronicRegen = 3; electronicTH = .7 * electronicMax; // 30% of eDamage can be handled by the ship this->loadModel("models/ships/mantawing.obj"); //this->setVisibiliy(false); bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false; this->setHealthMax(shieldMax); this->setHealth(shieldCur); this->travelNode = new PNode(); // camera - issue this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); // widget handling /* this->electronicWidget = new OrxGui::GLGuiEnergyWidgetVertical(); this->electronicWidget->setDisplayedName(std::string(this->getClassName()) + " Electronics:"); this->electronicWidget->setSize2D(30,400); this->electronicWidget->setAbsCoor2D(150,200); this->electronicWidget->shiftDir2D(270); this->updateElectronicWidget(); this->shieldWidget = new OrxGui::GLGuiEnergyWidgetVertical(); this->shieldWidget->setDisplayedName(std::string(this->getClassName()) + " Shield:"); this->shieldWidget->setSize2D(30,400); this->shieldWidget->setAbsCoor2D(200,200); this->shieldWidget->shiftDir2D(270); this->updateShieldWidget(); if (this->hasPlayer()) { State::getPlayer()->hud().setShiledWidget(this->shieldWidget); State::getPlayer()->hud().setEnergyWidget(this->electronicWidget); } */ this->electronicWidget = NULL; this->shieldWidget = NULL; //add events to the eventlist registerEvent(KeyMapper::PEV_FORWARD); registerEvent(KeyMapper::PEV_BACKWARD); registerEvent(KeyMapper::PEV_LEFT); registerEvent(KeyMapper::PEV_RIGHT); //registerEvent(SDLK_q); //registerEvent(SDLK_e); registerEvent(KeyMapper::PEV_FIRE1); registerEvent(KeyMapper::PEV_FIRE2); // Added for secondary weapon support registerEvent(KeyMapper::PEV_NEXT_WEAPON); registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); //registerEvent(SDLK_PAGEUP); //registerEvent(SDLK_PAGEDOWN); registerEvent(EV_MOUSE_MOTION); this->weaponMan.setParentEntity( this); this->secWeaponMan.setParentEntity( this); this->weaponMan.setSlotCount(6); this->weaponMan.setSlotPosition(0, Vector(0.0, 0, -3.0)); this->weaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); this->weaponMan.setSlotPosition(1, Vector(0.0, 0, 3.0)); this->weaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); this->weaponMan.setSlotPosition(2, Vector(-1.5, 0, -.5)); this->weaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); this->weaponMan.setSlotPosition(3, Vector(-1.5, 0, .5)); this->weaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); this->weaponMan.setSlotPosition(4, Vector(1.5, 0, .5)); this->weaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); this->weaponMan.setSlotPosition(5, Vector(1.5, 0, -.5)); this->weaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); this->secWeaponMan.setSlotCount(6); this->secWeaponMan.setSlotPosition(0, Vector(1.5, 0, 0)); this->secWeaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); this->secWeaponMan.setSlotPosition(1, Vector(2.6, 0, 3.0)); this->secWeaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); this->secWeaponMan.setSlotPosition(2, Vector(1.5, 0, -.5)); this->secWeaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); this->secWeaponMan.setSlotPosition(3, Vector(1.5, 0, .5)); this->secWeaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); this->secWeaponMan.setSlotPosition(4, Vector(1.5, 0, .5)); this->secWeaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); this->secWeaponMan.setSlotPosition(5, Vector(1.5, 0, -.5)); this->secWeaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); this->weaponMan.getFixedTarget()->setParent(this); this->weaponMan.getFixedTarget()->setRelCoor(100000,0,0); this->secWeaponMan.getFixedTarget()->setParent(this); this->secWeaponMan.getFixedTarget()->setRelCoor(100000,0,0); this->secWeaponMan.setRotationSpeed(0); dynamic_cast(this->weaponMan.getFixedTarget())->setVisibility( false); registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) ); registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) ); registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) ); registerVar( new SynchronizeableBool( &bFire, &bFire, "bSecFire", PERMISSION_OWNER)); registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity", PERMISSION_MASTER_SERVER ) ); //this->airFriction = 0.5f; this->travelDistancePlus = Vector2D(38.0, 43.0); this->travelDistanceMinus = Vector2D(-38.0, -43.0); this->cameraSpeed = 40; this->cameraLook = 0.0f; //this->airFriction = 0.0f; srand(time(0)); //initaialize RNG this->travelNode->debugDraw(); this->setSupportedPlaymodes(Playable::Horizontal | Playable::Vertical); this->trail = new Trail( 10, 10, .2, this); //this->trail->setParent( this); this->trail->setTexture( "maps/engine.png"); this->trailL = new Trail( 10, 10, .2, this); //this->trailL->setParent( this); this->trailL->setTexture( "maps/engine.png"); this->trailR = new Trail( 10, 10, .2, this); //this->trailR->setParent( this); this->trailR->setTexture( "maps/engine.png"); this->toList(OM_GROUP_00); //FIXME Just testaddition to show the wobblegrid this->test = new Wobblegrid(5); test->setTexture("maps/blub.png"); test->setAbsCoor( this->getAbsCoor() + Vector(0, 2, 0)); test->setParent( this); } /** * loads the Settings of a SpaceShip from an XML-element. * @param root the XML-element to load the Spaceship's properties from */ void SpaceShip::loadParams(const TiXmlElement* root) { Playable::loadParams(root); LoadParam(root, "playmode", this, SpaceShip, setPlaymodeXML); } void SpaceShip::setPlayDirection(const Quaternion& rot, float speed) { //this->direction = Quaternion (rot.getHeading(), Vector(0,1,0)); } void SpaceShip::reset() { bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false; //xMouse = yMouse = 0; this->setHealth(80); this->velocity = Vector(0.0, 0.0, 0.0); } void SpaceShip::enter() { this->secWeaponMan.showCrosshair(); this->toList( OM_GROUP_01 ); State::getPlayer()->hud().setOverlayActive(true); //dynamic_cast (State::getPlayer()->hud().getArmorWidget())->setDisplayedName("Armor"); //dynamic_cast(this->secWeaponMan.getFixedTarget())->setVisibility( true); //this->attachCamera(); // this->setPlaymode(Playable::Horizontal); } void SpaceShip::leave() { this->secWeaponMan.hideCrosshair(); this->toList( OM_GROUP_00); State::getPlayer()->hud().setOverlayActive(false); //dynamic_cast(this->secWeaponMan.getFixedTarget())->setVisibility( false); //this->detachCamera(); } /** * effect that occurs after the SpaceShip is spawned */ void SpaceShip::postSpawn () { //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); } /** * the action occuring if the spaceship left the game */ void SpaceShip::leftWorld () { } WorldEntity* ref = NULL; /** * this function is called, when two entities collide * @param entity: the world entity with whom it collides * * Implement behaviour like damage application or other miscellaneous collision stuff in this function */ void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location) { } /** * draws the spaceship after transforming it. */ void SpaceShip::draw () const { WorldEntity::draw(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); float matrix[4][4]; glTranslatef (this->getAbsCoor ().x-1, this->getAbsCoor ().y-.2, this->getAbsCoor ().z); this->getAbsDir().matrix (matrix); glMultMatrixf((float*)matrix); //glScalef(2.0, 2.0, 2.0); // no double rescale this->trail->draw(); glTranslatef(0,0,-.5); this->trailL->draw(); glTranslatef(0,0,1); this->trailR->draw(); glPopMatrix(); //this->debug(0); } /** * the function called for each passing timeSnap * @param time The timespan passed since last update */ void SpaceShip::tick (float time) { // Playable::tick(time);$ this->test->tick(time); // Own Tick Setup, as a different fire routine is used on the weapon manager this->weaponMan.tick(time); this->secWeaponMan.tick(time); if( this->systemFailure() ) bFire = bSecFire = false; // fire reqeust/release for primary weapons if( this->bFire) this->weaponMan.fire(); else this->weaponMan.releaseFire(); // fire reqeust/release for secondary weapons if( this->bSecFire) this->secWeaponMan.fire(); else this->secWeaponMan.releaseFire(); // Tracktick if(this->entityTrack) this->entityTrack->tick(time); // Shield Regeneration and other regular calculations on the ship this->regen(time); // Weapon Regeneration and other regular calculations on the ship this->weaponRegen(time); // current engine speed output this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare; // calculation of maxSpeed and acceleration: this->travelSpeed = this->engineSpeedCur * 5; this->acceleration = this->travelSpeed * 2; this->movement(time); // TRYING TO FIX PNode. this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f); this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f); this->velocity = (this->getAbsCoor() - this->oldPos) / time; this->oldPos = this->getAbsCoor(); this->trail->tick(time); this->trailL->tick(time); this->trailR->tick(time); this->weaponMan.setParentEntity( this); this->secWeaponMan.setParentEntity( this); //orient the spaceship in direction of the mouse /* rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia); if (this->getAbsDir().distance(rotQuat) > 0.00000000000001) this->setAbsDir( rotQuat); //this->setAbsDirSoft(mouseDir,5); */ // this is the air friction (necessary for a smooth control) /* if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001; else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001; */ //other physics (gravity) //if(travelSpeed < 120) //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time; //hoover effect //cycle += time; //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02); //readjust //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0))); //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0))); //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); /* this->shiftCoor(move); */ // PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber()); } /** * @todo switch statement ?? */ void SpaceShip::process(const Event &event) { //Playable::process(event); if( event.type == KeyMapper::PEV_LEFT) this->bLeft = event.bPressed; else if( event.type == KeyMapper::PEV_RIGHT) this->bRight = event.bPressed; else if( event.type == KeyMapper::PEV_FORWARD) this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); else if( event.type == KeyMapper::PEV_BACKWARD) this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); else if( event.type == KeyMapper::PEV_FIRE2) this->bSecFire = event.bPressed; else if( event.type == KeyMapper::PEV_FIRE1) this->bFire = event.bPressed; else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) { this->nextWeaponConfig(); } else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) this->previousWeaponConfig(); /* else if( event.type == EV_MOUSE_MOTION) { this->xMouse += event.xRel; this->yMouse += event.yRel; } */ } void SpaceShip::destroy( WorldEntity* killer ) { PRINTF(5)("spaceship destroy\n"); EmitterNode* node = NULL; DotEmitter* emitter = NULL; SpriteParticles* explosionParticles = NULL; explosionParticles = new SpriteParticles(200); explosionParticles->setName("SpaceShipExplosionParticles"); explosionParticles->setLifeSpan(.2, .3); explosionParticles->setRadius(0.0, 10.0); explosionParticles->setRadius(.5, 6.0); explosionParticles->setRadius(1.0, 3.0); explosionParticles->setColor(0.0, 1,1,1,.9); explosionParticles->setColor(0.1, 1,1,0,.9); explosionParticles->setColor(0.5, .8,.4,0,.5); explosionParticles->setColor(1.0, .2,.2,.2,.5); emitter = new DotEmitter( 2000, 70, 360); //emitter->setSpread( 0, M_2_PI); emitter->setEmissionRate( 200.0); //emitter->setEmissionVelocity( 200.0); //emitter->setSystem( explosionParticles); //emitter->setAbsCoor( this->getAbsCoor()); node = new EmitterNode( .1f); node->setupParticle( emitter, explosionParticles); node->setAbsDir( this->getAbsDir()); node->setVelocity( this->getVelocity() * .9f); node->setAbsCoor( this->getAbsCoor()); if( !node->start()) PRINTF(0)("Explosion node not correctly started!"); /* PNode* node = new PNode(); node->setAbsCoor(this->getAbsCoor()); Explosion* explosion = new Explosion(); explosion->explode( node, Vector(5,5,5)); */ if( this->hasPlayer()) { this->setAbsCoor(Vector(-10000,10000,10000)); this->hide(); } else { this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0))); } } void SpaceShip::respawn( ) { this->unhide(); /*for(ObjectList::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++) { if( dynamic_cast(*it) != NULL) dynamic_cast(*it)->toList( OM_GROUP_00); }*/ /* if( this->hasPlayer()) { this->toList( OM_GROUP_01); for(ObjectList::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++) { if( likely( dynamic_cast(*it) != NULL)) dynamic_cast(*it)->toList( OM_GROUP_00_PROJ); } } else { this->toList( OM_GROUP_00); for(ObjectList::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++) { if( likely( dynamic_cast(*it) != NULL)) dynamic_cast(*it)->toList( OM_GROUP_01_PROJ); } }*/ } void SpaceShip::damage(float pDamage, float eDamage){ PRINTF(5)("ship hit for (%f,%f) \n",pDamage,eDamage); if( this->shieldActive) { if( this->shieldCur > pDamage) { this->shieldCur = this->shieldCur - pDamage; } else { // shield <= pDamage this->shieldCur -=pDamage; this->shieldActive = false; //shield collapses pDamage += this->shieldCur; if( !this->shieldActive) { this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate this->electronicCur -= eDamage; } } } else { this->armorCur = this->armorCur - pDamage; this->electronicCur = this->electronicCur - eDamage; } if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ } this->destroy(this); updateElectronicWidget(); updateShieldWidget(); this->setHealth( this->armorCur); } void SpaceShip::regen(float time){ float tmp; if (this->armorCur != this->armorMax || this->armorRegen != 0){ tmp = this->armorCur + this->armorRegen * time; if ( tmp > electronicMax) this->armorCur = this->armorMax; else this->armorCur = tmp; } if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){ tmp = this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time; if( tmp > shieldMax) this->shieldCur = this->shieldMax; else this->shieldCur = tmp; this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH); updateShieldWidget(); } this->setHealth( this->shieldCur); // FIXME currently just to test share system if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){ tmp = this->electronicCur + this->electronicRegen * time; if ( tmp > electronicMax) this->electronicCur = this->electronicMax; else this->electronicCur = tmp; updateElectronicWidget(); } } /** * Weapon regeneration * does not use any reactor capacity, as it wouldn't work in a consistent way. */ void SpaceShip::weaponRegen(float time) { float energy = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time; Weapon* weapon; for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++) { weapon = this->weaponMan.getWeapon(i); if( weapon != NULL && weapon->isActive()) { weapon->increaseEnergy( energy); } } // weaponMan.increaseAmmunition( weapon, energy); } void SpaceShip::enterPlaymode(Playable::Playmode playmode) { switch(playmode) { case Playable::Full3D: /* if (State::getCameraNode != NULL) { Vector absCoor = this->getAbsCoor(); this->setParent(PNode::getNullParent()); this->setAbsCoor(absCoor); State::getCameraNode()->setParentSoft(&this->cameraNode); State::getCameraNode()->setRelCoorSoft(-10, 0,0); State::getCameraTargetNode()->setParentSoft(&this->cameraNode); State::getCameraTargetNode()->setRelCoorSoft(100, 0,0); } */ //break; break; case Playable::Horizontal: if (State::getCameraNode != NULL) { this->debugNode(1); this->travelNode->debugNode(1); this->travelNode->setAbsCoor(this->getAbsCoor()); this->travelNode->updateNode(0.01f); this->setParent(this->travelNode); this->setRelCoor(0,0,0); State::getCameraNode()->setParentSoft(this->travelNode); //State::getCameraNode()->setParentSoft(this); State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0); State::getCameraTargetNode()->setParentSoft(this->travelNode); //State::getCameraTargetNode()->setParentSoft(this); State::getCameraTargetNode()->setRelCoorSoft(0,0,0); this->debugNode(1); this->travelNode->debugNode(1); } break; default: PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName()); } } /** * @brief calculate the velocity * @param time the timeslice since the last frame */ void SpaceShip::movement (float dt) { //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so //the ship doesn't slide too much. float airCoeff = 2.5; float pi = 3.14; switch(this->getPlaymode()) { case Playable::Horizontal: { // these routines will change the travel movement into zero in a short amout of time, if the player // doesn't press any buttons. if (this->travelVelocity.x >= 0) { if (this->travelVelocity.x > airCoeff*this->acceleration * dt) this->travelVelocity.x -= airCoeff* this->acceleration * dt; else this->travelVelocity.x = 0; } else { if (this->travelVelocity.x < -airCoeff*this->acceleration * dt) this->travelVelocity.x += airCoeff* this->acceleration * dt; else this->travelVelocity.x = 0; } if (this->travelVelocity.z >= 0) { if (this->travelVelocity.z > airCoeff*this->acceleration * dt) this->travelVelocity.z -= airCoeff* this->acceleration * dt; else this->travelVelocity.z = 0; } else { if (this->travelVelocity.z < -airCoeff*this->acceleration * dt) this->travelVelocity.z += airCoeff* this->acceleration * dt; else this->travelVelocity.z = 0; } // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons // and its continuing to slide a bit. Vector oldCoor = this->getRelCoor(); if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z); if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z); if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y); if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y); if( this->systemFailure() ) bForward = bBackward = bLeft = bRight = false; if( this->bForward ) { if(this->getRelCoor().x < this->travelDistancePlus.x) { if (this->travelVelocity.x < this->travelSpeed) { this->travelVelocity.x += (airCoeff + 1.0)*this->acceleration*dt; } else { this->travelVelocity.x = this->travelSpeed; } } else { this->travelVelocity.x = 0.0f; } } if( this->bBackward ) { if(this->getRelCoor().x > this->travelDistanceMinus.x) { if (this->travelVelocity.x > -this->travelSpeed) { this->travelVelocity.x -= (airCoeff + 1.0)*this->acceleration*dt; } else { this->travelVelocity.x = -this->travelSpeed; } } else { this->travelVelocity.x = 0.0f; } } if( this->bLeft) { if(this->getRelCoor().z > this->travelDistanceMinus.y) { if (this->travelVelocity.z > -this->travelSpeed) { this->travelVelocity.z -= (airCoeff + 1.0)*this->acceleration*dt; } else { this->travelVelocity.z = -this->travelSpeed; } } else { this->travelVelocity.z = 0.0f; } this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 6); } if( this->bRight) { if(this->getRelCoor().z < this->travelDistancePlus.y) { if (this->travelVelocity.z < this->travelSpeed) { this->travelVelocity.z += (airCoeff + 1.0)*this->acceleration*dt; } else { this->travelVelocity.z = this->travelSpeed; } } else { this->travelVelocity.z = 0.0f; } this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 6); } if (!this->bRight && !this->bLeft) { this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 6); } //normalisation of the vectors (vector sum must be <= travelspeed) float xzNorm = sqrt(pow(this->travelVelocity.x, 2) + pow(this->travelVelocity.z, 2)); if (xzNorm > this->travelSpeed) { this->travelVelocity.x = this->travelVelocity.x/xzNorm * this->travelSpeed; this->travelVelocity.z = this->travelVelocity.z/xzNorm * this->travelSpeed; } //this moves camera and ship along the travel path. this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0)); break; } case Playable::Vertical: break; default: PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName()); } //set new coordinates calculated through key- events. this->shiftCoor (this->travelVelocity * dt); } void SpaceShip::setPlaymodeXML(const std::string& playmode) { this->setPlaymode(Playable::stringToPlaymode(playmode)); } /** * @brief jumps to the next WeaponConfiguration */ void SpaceShip::nextWeaponConfig() { PRINTF(0)("Requested next weapon config!\n"); this->weaponMan.nextWeaponConfig(); Playable::weaponConfigChanged(); } /** * @brief moves to the last WeaponConfiguration */ void SpaceShip::previousWeaponConfig() { this->curWeaponPrimary = (this->curWeaponPrimary + 1) % 3; this->weaponMan.changeWeaponConfig(this->curWeaponPrimary); Playable::weaponConfigChanged(); } void SpaceShip::hit(float damage, WorldEntity* killer) { this->damage(killer->getDamage(),0); killer->collidesWith( this, this->getAbsCoor()); //this->collidesWith(killer, this->getAbsCoor()); } void SpaceShip::updateElectronicWidget() { if (this->electronicWidget != NULL) { //if it exists already: update it this->electronicWidget->setMaximum(this->electronicMax); this->electronicWidget->setValue(this->electronicCur); } else { //create the widget this->electronicWidget = new OrxGui::GLGuiEnergyWidgetVertical(); this->electronicWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1)); //this->electronicWidget->setDisplayedName("Electronics:"); //this->electronicWidget->setSize2D(100,20); //this->electronicWidget->setAbsCoor2D(150,200); this->updateElectronicWidget(); if (this->hasPlayer()) State::getPlayer()->hud().setEnergyWidget(this->electronicWidget); } } void SpaceShip::updateShieldWidget() { if (this->shieldWidget != NULL) { this->shieldWidget->setMaximum(this->shieldMax); this->shieldWidget->setValue(this->shieldCur);; } else { this->shieldWidget = new OrxGui::GLGuiEnergyWidgetVertical(); this->shieldWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1)); //this->shieldWidget->setDisplayedName("Shield:"); //his->shieldWidget->setSize2D(100,20); //this->shieldWidget->setAbsCoor2D(200,200); this->updateShieldWidget(); if (this->hasPlayer()) State::getPlayer()->hud().setShiledWidget(this->shieldWidget); } }