Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weapons/laser.cc @ 5516

Last change on this file since 5516 was 5512, checked in by bensch, 19 years ago

orxonox/trunk: removed a segfault, that came through the call to BaseObject::exists(); in Laser.h

File size: 4.0 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific
12   main-programmer: Patrick Boenzli
13   co-programmer: Benjamin Grauer
14
15*/
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
17
18#include "laser.h"
19
20#include "fast_factory.h"
21
22#include "state.h"
23#include "list.h"
24#include "class_list.h"
25#include "model.h"
26
27#include "particle_engine.h"
28#include "particle_emitter.h"
29#include "particle_system.h"
30
31
32using namespace std;
33
34CREATE_FAST_FACTORY_STATIC(Laser, CL_LASER);
35
36/**
37 *  standard constructor
38*/
39Laser::Laser () : Projectile()
40{
41  this->setClassID(CL_LASER, "Laser");
42
43  float modelSize = .3;
44  this->loadModel("models/projectiles/laser.obj");
45
46  this->energyMin = 1;
47  this->energyMax = 10;
48  this->remove();
49  this->lifeSpan = 1.0;
50
51  this->emitter = new ParticleEmitter(Vector(0,1,0), M_2_PI, 100, 5);
52  this->emitter->setParent(this);
53  this->emitter->setSpread(M_PI, M_PI);
54  this->emitter->setEmissionRate(300.0);
55  this->emitter->setEmissionVelocity(50.0);
56}
57
58
59/**
60 *  standard deconstructor
61*/
62Laser::~Laser ()
63{
64  // delete this->emitter;
65
66  /* this is normaly done by World.cc by deleting the ParticleEngine */
67  if (Laser::explosionParticles != NULL && ClassList::getList(CL_LASER)->getSize() <= 1)
68  {
69    //if (ClassList::exists(Laser::explosionParticles, CL_PARTICLE_SYSTEM))
70    //  delete Laser::explosionParticles;
71    PRINTF(1)("Deleting Laser Particles\n");
72    Laser::explosionParticles = NULL;
73  }
74
75}
76
77ParticleSystem* Laser::explosionParticles = NULL;
78
79void Laser::activate()
80{
81  State::getWorldEntityList()->add(this);
82  if (unlikely(Laser::explosionParticles == NULL))
83  {
84    Laser::explosionParticles = new ParticleSystem(1000, PARTICLE_SPRITE);
85    Laser::explosionParticles->setName("LaserExplosionParticles");
86    Laser::explosionParticles->setLifeSpan(.5, .3);
87    Laser::explosionParticles->setRadius(0.0, 3.0);
88    Laser::explosionParticles->setRadius(.5, 6.0);
89    Laser::explosionParticles->setRadius(1.0, 3.0);
90    Laser::explosionParticles->setColor(0.0, 1,1,0,.9);
91    Laser::explosionParticles->setColor(0.5, .8,.8,0,.5);
92    Laser::explosionParticles->setColor(1.0, .8,.8,.7,.0);
93  }
94}
95
96
97void Laser::deactivate()
98{
99  ParticleEngine::getInstance()->breakConnections(this->emitter);
100  this->lifeCycle = 0.0;
101
102//  GarbageCollector::getInstance()->collect(this);
103  State::getWorldEntityList()->remove(this);
104  Laser::fastFactory->kill(this);
105}
106
107
108void Laser::collidesWith(WorldEntity* entity, const Vector& location)
109{
110  if (this->hitEntity != entity && entity->isA(CL_NPC))
111    this->destroy();
112  this->hitEntity = entity;
113}
114
115/**
116 *  signal tick, time dependent things will be handled here
117 * @param time since last tick
118*/
119void Laser::tick (float time)
120{
121  //Vector v = *this->flightDirection * ( this->speed * time * 1000 + 0.1);
122  Vector v = this->velocity * (time);
123  this->shiftCoor(v);
124
125  this->lifeCycle += time/this->lifeSpan;
126  if( this->lifeCycle >= 1.0)
127    {
128      PRINTF(5)("FINALIZE==========================\n");
129      PRINTF(5)("current life cycle is: %f\n", this->lifeCycle);
130      PRINTF(5)("FINALIZE===========================\n");
131
132      this->deactivate();
133    }
134}
135
136/**
137 *  the function gets called, when the projectile is destroyed
138*/
139void Laser::destroy ()
140{
141  PRINTF(5)("DESTROY Laser\n");
142  this->lifeCycle = .95; //!< @todo calculate this usefully.
143  ParticleEngine::getInstance()->addConnection(this->emitter, Laser::explosionParticles);
144
145}
146
147
148void Laser::draw () const
149{
150  glMatrixMode(GL_MODELVIEW);
151  glPushMatrix();
152  glPushAttrib(GL_ENABLE_BIT);
153  glDisable(GL_LIGHTING);
154
155  float matrix[4][4];
156  glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z);
157  this->getAbsDir().matrix (matrix);
158  glMultMatrixf((float*)matrix);
159  glScalef(2.0, 2.0, 2.0);
160  this->model->draw();
161  glPopAttrib();
162  glPopMatrix();
163}
164
Note: See TracBrowser for help on using the repository browser.