Enemy.cpp
资源名称:g.rar [点击查看]
上传用户:laitongbao
上传日期:2021-02-20
资源大小:8176k
文件大小:3k
源码类别:
射击游戏
开发平台:
Visual C++
- // Enemy.cpp: implementation of the Enemy class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Enemy.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- #include "Exploder.h"
- #include "LaserGun.h"
- #include "playership.h"
- #include "MachineGun.h"
- #include "math.h"
- Enemy1::Enemy1(VECTOR3 pos0,VECTOR3 v0,int bonus)//bonus是否死后会掉奖励
- {
- mesh = (MeshObject*)g_tree->AddObject(new MeshObject("enemy1",pos0),"base");
- mesh->SetVelocity(v0);
- mesh->LoadMeshData("enemy1.x");
- this->bonus = bonus;
- expInner = new Exploder(6.0f,40,15.f);
- expOuter = new Exploder(1.0f,50,100.f);
- expBengin = new Exploder(3.f,10,10.f);
- this->gun1 = new LaserGun(this->mesh ,3.f);
- this->gun2 = new LaserGun(this->mesh ,-3.f);
- this->deadStayTimer = new FrameTimer(30);
- this->alive = true;
- this->active = true;
- shell = 10;
- t0 = 0;
- angle = atanf(v0.x/-v0.z);
- whichGunToFire = true;
- }
- void Enemy1::Damage(int dam)
- {
- this->shell -= dam;
- if(shell<=0&&active)
- {
- deadStayTimer->startTimer();
- expBengin->Explode(TransitionController::GetWorldPosition(mesh));
- mesh->SetRotateVelocity(VECTOR3(-2.f,0.f,-0.5f));
- active = false;
- points += 100;//得分
- }
- }
- void Enemy1::TurnLeft()
- {
- if(!active)
- return;
- angle -= g_timer->GetFrameTime()*0.1f;
- TransitionController::SetRotation(mesh,VECTOR3(0.f,-angle,0.f));
- VECTOR3 v;
- VECTOR3 currentspeed = mesh->GetVelocity();
- v.x = currentspeed.Length()*sinf(angle);
- v.z = -currentspeed.Length()*cosf(angle);
- mesh->SetVelocity(v);
- }
- void Enemy1::TurnRight()
- {
- if(!active)
- return;
- angle += g_timer->GetFrameTime()*0.2f;
- TransitionController::SetRotation(mesh,VECTOR3(0.f,-angle,0.f));
- VECTOR3 v;
- VECTOR3 currentspeed = mesh->GetVelocity();
- v.x = currentspeed.Length()*sinf(angle);
- v.z = -currentspeed.Length()*cosf(angle);
- mesh->SetVelocity(v);
- }
- void Enemy1::DoFrame()
- {
- //go toward player
- VECTOR3 v = g_pship->mesh->GetWorldPos() - mesh->GetWorldPos();
- float current_angle = atanf(v.x/-v.z);
- if(current_angle < angle)
- {
- TurnLeft();
- }else
- {
- TurnRight();
- }
- //fire
- if(g_timer->GetTime () - t0 >1.0f&&active&¤t_angle - angle<0.1f&¤t_angle - angle>-0.1f)
- {
- VECTOR3 direction = TransitionController::GetWorldPosition(g_pship->mesh)-TransitionController::GetWorldPosition(mesh);
- if(direction.Length()<300.f)//fire when get closed
- {
- //g_enemyshotsound->PlaySound();
- if(whichGunToFire)
- this->gun1->Fire(direction);
- else
- this->gun2->Fire(direction);
- }
- whichGunToFire = !whichGunToFire;
- t0 = g_timer->GetTime ();
- }
- //dying
- if(deadStayTimer->isRunning())
- {
- deadStayTimer->update();
- if(deadStayTimer->reach())
- {
- g_expsound->PlaySound();
- this->expInner->Explode(TransitionController::GetWorldPosition(mesh));
- this->expOuter->Explode(TransitionController::GetWorldPosition(mesh));
- deadStayTimer->stopTimer();
- this->alive = false;
- }
- }
- }
- Enemy1::~Enemy1()
- {
- delete gun1;
- delete gun2;
- delete expOuter;
- delete expInner;
- g_tree->DeleteObject(mesh);
- mesh = NULL;
- }