Jump to content

aiaf's Blog

  • entries
    11
  • comments
    4
  • views
    7,354

[Example] Basic missile class


aiaf

1,535 views

 Share

Want to share this maybe will help someone.

 

Its pretty basic but it has missile effect by distance and at certain time.

Should be easy to extend.

 

trigger the missile:

 

tm3 = new Missile(Vec3(5.0f, 5.0f, 0.0f));
tm2 = new Missile(Vec3(0.0f, 0.0f, 0.0f), tm3->GetPosition(), true);

 

and call the Loop method in the game loop

 

#ifndef __S_MISSILE_H__
#define __S_MISSILE_H__
#include "Leadwerks.h"

using namespace Leadwerks;
class Missile {
private:
 Emitter *mem;
 Material* unitexpmat;
 Model *model;
 Vec3 targetDirection;
 float mass;
 long creationTime;
 long detonationTime;
 void Init();
 void Effect(Material* mat);
public:
 Missile();
 Missile(Vec3 pos);
 Missile(Vec3 pos, Vec3 targetDirection, bool launch);
 void SetDetonationTime(long detonationTime);
 void SetPosition(Vec3 pos);
 void SetDirection(Vec3 targetDirection);
 void SetMass(float mass);
 Vec3 GetPosition();
 Vec3 GetTargetPosition();
 void Launch();
 void Loop(Material* mat);
};
#endif

 

#include "Missile.h"
void Missile::Init() {
model = Model::Box(0.1, 0.1, 0.1);
model->SetColor(0.1, 0.1, 0.1);
mass = 0.01;
model->SetMass(mass);
model->SetGravityMode(false);
Shape* shape = Shape::Box(0,0,0,0,0,0,0.1, 0.1, 0.1);
model->SetShape(shape);
shape->Release();
creationTime = Time::Millisecs();
detonationTime = -1;
}
Missile::Missile() {
Init();
}
Missile::Missile(Vec3 pos, Vec3 direction, bool launch) {
Init();
SetPosition(pos);
SetDirection(direction);
if(launch == true) {
 model->AddForce(direction[0], direction[1], direction[2], true);
}
}
Missile::Missile(Vec3 pos) {
Init();
SetPosition(pos);
}
void Missile::SetPosition(Vec3 pos) {
model->SetPosition(pos);
}
void Missile::SetDirection(Vec3 targetDirection) {
this->targetDirection = targetDirection;
}
void Missile::SetMass(float mass) {
this->mass = mass;
}
void Missile::SetDetonationTime(long detonationTime) {
this->detonationTime = (creationTime + detonationTime);
}

Vec3 Missile::GetPosition() {
return model->GetPosition();
}
Vec3 Missile::GetTargetPosition() {
return targetDirection;
}
void Missile::Launch() {
model->AddForce(targetDirection[0], targetDirection[1], targetDirection[2], false);
}
void Missile::Effect(Material* mat) {
 Vec3 nd = targetDirection.Normalize();
 mem = Emitter::Create(200);
 mem->SetMaterial(mat);
 mem->SetEmissionShape(1);
 mem->SetEmissionVolume(Vec3(.50,.50,.50));
 mem->SetDuration(300);
 mem->SetVelocity(nd[0],nd[1],nd[2],0);
 mem->SetColor(.30,.0,.0,.5,0);
 mem->SetColor(.20,.10,.10,0.4,1);
 mem->AddScaleControlPoint(0,0);
 mem->AddScaleControlPoint(.9,3);
 mem->AddScaleControlPoint(0.1,0);
 mem->SetLoopMode(false);
}
void Missile::Loop(Material* mat) {
if(detonationTime > 0) {
 if(Time::Millisecs() >= detonationTime) {
	 Effect(mat);
	 mem->SetPosition(GetPosition()[0], GetPosition()[1], GetPosition()[2],true);
	 model->Hide();
	 detonationTime = -1;
 }
}
else {
 if(model->GetPosition().DistanceToPoint(targetDirection) < 0.5) {
	 Effect(mat);
	 mem->SetPosition(targetDirection[0], targetDirection[1], targetDirection[2],true);
	 model->Hide();
 }
}
}

  • Upvote 4
 Share

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...