Torque3D Documentation / _generateds / afxEA_MachineGun.cpp

afxEA_MachineGun.cpp

Engine/source/afx/ea/afxEA_MachineGun.cpp

More...

Classes:

Detailed Description

  1
  2
  3//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  4// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  5// Copyright (C) 2015 Faust Logic, Inc.
  6//
  7// Permission is hereby granted, free of charge, to any person obtaining a copy
  8// of this software and associated documentation files (the "Software"), to
  9// deal in the Software without restriction, including without limitation the
 10// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 11// sell copies of the Software, and to permit persons to whom the Software is
 12// furnished to do so, subject to the following conditions:
 13//
 14// The above copyright notice and this permission notice shall be included in
 15// all copies or substantial portions of the Software.
 16//
 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 23// IN THE SOFTWARE.
 24//
 25//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
 26
 27#include <typeinfo>
 28#include "afx/arcaneFX.h"
 29#include "afx/afxEffectDefs.h"
 30#include "afx/afxEffectWrapper.h"
 31#include "afx/afxChoreographer.h"
 32#include "afx/ce/afxProjectile.h"
 33#include "afx/ce/afxMachineGun.h"
 34
 35//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
 36// afxEA_MachineGun 
 37
 38class afxEA_MachineGun : public afxEffectWrapper
 39{
 40  typedef afxEffectWrapper Parent;
 41
 42  afxMachineGunData* gun_data;
 43  ProjectileData*    bullet_data;
 44
 45  bool          shooting;
 46  F32           start_time;
 47  F32           shot_gap;
 48  S32           shot_count;
 49
 50  void          launch_projectile();
 51  void          do_runtime_substitutions();
 52
 53public:
 54  /*C*/         afxEA_MachineGun();
 55  /*D*/         ~afxEA_MachineGun();
 56
 57  virtual void  ea_set_datablock(SimDataBlock*);
 58  virtual bool  ea_start();
 59  virtual bool  ea_update(F32 dt);
 60  virtual void  ea_finish(bool was_stopped);
 61};
 62
 63//~~~~~~~~~~~~~~~~~~~~//
 64
 65afxEA_MachineGun::afxEA_MachineGun()
 66{
 67  gun_data = 0;
 68  bullet_data = 0;
 69  shooting = false;
 70  start_time = 0.0f;
 71  shot_count = 0;
 72  shot_gap = 0.2f;
 73}
 74
 75afxEA_MachineGun::~afxEA_MachineGun()
 76{
 77  if (gun_data && gun_data->isTempClone())
 78    delete gun_data;
 79  gun_data = 0;
 80}
 81
 82void afxEA_MachineGun::ea_set_datablock(SimDataBlock* db)
 83{
 84  gun_data = dynamic_cast<afxMachineGunData*>(db);
 85  if (gun_data)
 86    bullet_data = gun_data->projectile_data;
 87}
 88
 89bool afxEA_MachineGun::ea_start()
 90{
 91  if (!gun_data)
 92  {
 93    Con::errorf("afxEA_MachineGun::ea_start() -- missing or incompatible datablock.");
 94    return false;
 95  }
 96  if (!bullet_data)
 97  {
 98    Con::errorf("afxEA_MachineGun::ea_start() -- missing or incompatible ProjectileData.");
 99    return false;
100  }
101
102  do_runtime_substitutions();
103
104  if (gun_data->rounds_per_minute > 0)
105    shot_gap = 60.0f/gun_data->rounds_per_minute;
106
107  return true;
108}
109
110bool afxEA_MachineGun::ea_update(F32 dt)
111{
112  if (!shooting)
113  {
114    start_time = mElapsed;
115    shooting = true;
116  }
117  else
118  {
119    F32 next_shot = start_time + (shot_count+1)*shot_gap;
120    while (next_shot < mElapsed)
121    {
122      if (mIn_scope)
123        launch_projectile();
124      next_shot += shot_gap;
125      shot_count++;
126    }
127  }
128
129  return true;
130}
131
132void afxEA_MachineGun::ea_finish(bool was_stopped)
133{
134}
135
136void afxEA_MachineGun::launch_projectile()
137{
138  afxProjectile* projectile = new afxProjectile();
139
140  ProjectileData* next_bullet = bullet_data;
141
142  if (bullet_data->getSubstitutionCount() > 0)
143  {
144    next_bullet = new ProjectileData(*bullet_data, true);
145    bullet_data->performSubstitutions(next_bullet, mChoreographer, mGroup_index);
146  }
147
148  projectile->onNewDataBlock(next_bullet, false);
149
150  F32 muzzle_vel = next_bullet->muzzleVelocity;
151
152  afxConstraint* pos_cons = getPosConstraint();
153  ShapeBase* src_obj = (pos_cons) ? (dynamic_cast<ShapeBase*>(pos_cons->getSceneObject())) : 0;
154
155  Point3F dir_vec = mUpdated_aim - mUpdated_pos;
156  dir_vec.normalizeSafe();
157  dir_vec *= muzzle_vel;
158  projectile->init(mUpdated_pos, dir_vec, src_obj);
159  if (!projectile->registerObject())
160  {
161    delete projectile;
162    projectile = 0;
163    Con::errorf("afxEA_MachineGun::launch_projectile() -- projectile failed to register.");
164  }
165  if (projectile)
166    projectile->setDataField(StringTable->insert("afxOwner"), 0, mChoreographer->getIdString());
167}
168
169void afxEA_MachineGun::do_runtime_substitutions()
170{
171  // only clone the datablock if there are substitutions
172  if (gun_data->getSubstitutionCount() > 0)
173  {
174    // clone the datablock and perform substitutions
175    afxMachineGunData* orig_db = gun_data;
176    gun_data = new afxMachineGunData(*orig_db, true);
177    orig_db->performSubstitutions(gun_data, mChoreographer, mGroup_index);
178  }
179}
180
181//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
182
183class afxEA_MachineGunDesc : public afxEffectAdapterDesc, public afxEffectDefs 
184{
185  static afxEA_MachineGunDesc desc;
186
187public:
188  virtual bool  testEffectType(const SimDataBlock*) const;
189  virtual bool  requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
190  virtual bool  runsOnServer(const afxEffectWrapperData*) const { return true; }
191  virtual bool  runsOnClient(const afxEffectWrapperData*) const { return false; }
192
193  virtual afxEffectWrapper* create() const { return new afxEA_MachineGun; }
194};
195
196afxEA_MachineGunDesc afxEA_MachineGunDesc::desc;
197
198bool afxEA_MachineGunDesc::testEffectType(const SimDataBlock* db) const
199{
200  return (typeid(afxMachineGunData) == typeid(*db));
201}
202
203bool afxEA_MachineGunDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
204{
205  return (timing.lifetime < 0);
206}
207
208//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
209