PlayerShip.cpp
资源名称:g.rar [点击查看]
上传用户:laitongbao
上传日期:2021-02-20
资源大小:8176k
文件大小:5k
源码类别:
射击游戏
开发平台:
Visual C++
- // PlayerShip.cpp: implementation of the PlayerShip class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "PlayerShip.h"
- #include "Exploder.h"
- #include "MachineGun.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- Force *xengine ;
- Force *zengine ;
- Resistance * ship_resistance;
- PlayerShip::PlayerShip()
- {
- static bool init = false;
- if(!init)
- {
- xengine = (Force*)g_tree->AddObject( &Force("xengine"),(NodeObject*)NULL);
- zengine = (Force*)g_tree->AddObject( &Force("zengine"),(NodeObject*)NULL);
- ship_resistance = (Resistance*)g_tree->AddObject ( &Resistance("shipResistance"),(NodeObject*)NULL);
- init = true;
- }
- mesh = (MeshObject*)g_tree->AddObject(&MeshObject("playership",VECTOR3(0,0,0)),"base");
- wake = (Particle_Emit*)g_tree->AddObject(&Particle_Emit("wake",5.f,0.05f,"Fire\wake.bmp",
- 0xff2266ff,0xff664400,VECTOR3(0.0f,0.0f,-5.0f)),mesh);
- mesh->LoadMeshData("x_wings.x");
- mesh->SetMass(100.f);
- TransitionController::SetRotation(mesh,VECTOR3(0.f,0.f,0.f));
- gun1 = new MachineGun(mesh->GetName(),-1.f);
- gun2 = new MachineGun(mesh->GetName(),1.f);
- explode = new Exploder(15.f,10,20.f);
- explode2 = new Exploder(1.f,50,300.f);
- expBengin = new Exploder(10.f,20,10.f);
- shell = 100;
- xengine->SetPower (200.f);
- zengine->SetPower (100.f);
- xengine->AddObject(mesh);
- zengine->AddObject(mesh);
- ship_resistance->AddObject(mesh);
- ship_resistance->SetPower(0.03f);
- this->deadStayTimer = new FrameTimer(80);
- active = true;
- }
- void PlayerShip::Reset()
- {
- mesh->SetVisible (true);
- wake->SetVisible (true);
- gun1->emit->SetVisible(true);
- gun2->emit->SetVisible(true);
- TransitionController::SetPosition(mesh,VECTOR3());
- TransitionController::SetRotation(mesh,VECTOR3());
- mesh->SetRotateVelocity(VECTOR3());
- active = true;
- wake->SetSize(5.f);
- wake->SetLifeTime(0.05f);
- shell = 100;
- explode ->Reset();
- explode2 ->Reset();
- expBengin->Reset();
- }
- void PlayerShip::Fire()
- {
- static float t = 0;
- if(g_timer->GetTime () - t >0.10f&&active)
- {
- g_playershotsound->PlaySound();
- this->gun2->Fire();
- this->gun1->Fire();
- t = g_timer->GetTime ();
- }
- }
- void PlayerShip::Bob()
- {
- }
- void PlayerShip::MoveLeft()
- {
- if(!active)
- return;
- VECTOR3 rv = TransitionController::GetRotation(mesh);
- if(rv.z>PI/12)
- {
- xengine->SetDirection(VECTOR3(-1.f,0.f,0.f));
- xengine->Enable();
- }
- else
- {
- xengine->Disable();
- }
- if(rv.z<PI/6)
- mesh->SetRotateVelocity(VECTOR3(0.f,0.f,2.f));
- else
- mesh->SetRotateVelocity(VECTOR3(0.f,0.f,0.f));
- }
- void PlayerShip::MoveRight()
- {
- if(!active)
- return;
- VECTOR3 rv = TransitionController::GetRotation(mesh);
- if(rv.z<-PI/12)
- {
- xengine->SetDirection(VECTOR3(1.f,0.f,0.f));
- xengine->Enable();
- }
- else
- {
- xengine->Disable();
- }
- if(rv.z>-PI/6)
- mesh->SetRotateVelocity(VECTOR3(0.f,0.f,-2.f));
- else
- mesh->SetRotateVelocity(VECTOR3(0.f,0.f,0.f));
- }
- void PlayerShip::RotateBack()
- {
- if(!active)
- return;
- VECTOR3 rv = TransitionController::GetRotation(mesh);
- if(rv.z > 0.1f)
- {
- mesh->SetRotateVelocity(VECTOR3(0.f,0.f,-2.f));
- }
- else if(rv.z <-0.1f)
- {
- mesh->SetRotateVelocity(VECTOR3(0.f,0.f,2.f));
- }else
- {
- mesh->SetRotateVelocity(VECTOR3(0.f,0.f,0.f));
- }
- }
- void PlayerShip::MoveForward()
- {
- if(!active)
- return;
- zengine->SetDirection(VECTOR3(0.f,0.f,1.f));
- wake->SetSize(8.f);
- zengine->Enable();
- }
- void PlayerShip::MoveBackward()
- {
- if(!active)
- return;
- zengine->SetDirection(VECTOR3(0.f,0.f,-1.f));
- wake->SetSize(4.f);
- zengine->Enable();
- }
- void PlayerShip::ZengineStop()
- {
- zengine->Disable();
- wake->SetSize(5.f);
- }
- void PlayerShip::XengineStop()
- {
- xengine->Disable();
- }
- void PlayerShip::DoFrame()
- {
- if(deadStayTimer->isRunning())
- {
- deadStayTimer->update();
- mesh->SetRotateVelocity(VECTOR3(0.1f,0.f,-1.f));
- if(deadStayTimer->reach())
- {
- Die();
- deadStayTimer->stopTimer();
- Reset();
- }
- }
- VECTOR3 pv = TransitionController::GetPosition(mesh);
- if(pv.x>50.f)
- {
- pv.x = 50.f;
- TransitionController::SetPosition(mesh,pv);
- }
- if(pv.x<-50.f)
- {
- pv.x = -50.f;
- TransitionController::SetPosition(mesh,pv);
- }
- if(pv.z<-30.f)
- {
- pv.z = -30.f;
- TransitionController::SetPosition(mesh,pv);
- }
- if(pv.z>100.f)
- {
- pv.z = 100.f;
- TransitionController::SetPosition(mesh,pv);
- }
- wake->Emit(VECTOR3(0,0,-100));
- }
- void PlayerShip::Damage(int value)
- {
- shell -= value;
- if(shell<=0&&active)
- {
- deadStayTimer->startTimer();
- this->expBengin->Explode(mesh->GetWorldPos());
- wake->SetSize(10.f);
- wake->SetLifeTime(0.2f);
- active = false;
- }
- }
- void PlayerShip::Die()
- {
- g_expsound->PlaySound();
- this->explode->Explode(mesh->GetWorldPos());
- this->explode2->Explode(mesh->GetWorldPos());
- mesh->SetVisible (false);
- wake->SetVisible (false);
- gun1->emit->SetVisible(false);
- gun2->emit->SetVisible(false);
- }
- void PlayerShip::PowerUp()
- {
- gun1->PowerUp();
- gun2->PowerUp();
- }
- PlayerShip::~PlayerShip()
- {
- delete explode;
- delete explode2;
- delete expBengin;
- delete deadStayTimer;
- }