Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/vs-enhencements/src/world_entities/npcs/actionbox_enemy.cc @ 10662

Last change on this file since 10662 was 10662, checked in by rennerc, 17 years ago

Vertical playmode for SpaceShip

File size: 4.6 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: Christoph Renner
13   co-programmer:
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
18#include "loading/factory.h"
19#include "debug.h"
20#include "loading/load_param.h"
21#include "util/loading/load_param_xml.h"
22#include "state.h"
23#include "player.h"
24#include "playable.h"
25
26#include "actionbox_enemy.h"
27
28ObjectListDefinition(ActionboxEnemy);
29CREATE_FACTORY(ActionboxEnemy);
30
31ActionboxEnemy::ActionboxEnemy(const TiXmlElement* root)
32{
33  PRINTF(0)("ActionboxEnemy\n");
34 
35  this->registerObject(this, ActionboxEnemy::_objectList);
36  this->toList(OM_GROUP_00);
37 
38  this->isActive = true;
39 
40  this->pitch = 0.0f;
41  this->dPitch = 0.0;
42 
43  this->maxSpeed = 10;
44  this->acceleration = 3;
45  this->speed = 0;
46 
47  if ( root )
48    this->loadParams( root );
49}
50
51ActionboxEnemy::~ActionboxEnemy()
52{
53}
54
55void ActionboxEnemy::loadParams(const TiXmlElement* root)
56{
57  WorldEntity::loadParams( root );
58}
59
60void ActionboxEnemy::tick( float dt )
61{
62  myDir = this->getAbsDir();
63  myCoor = this->getAbsCoor();
64 
65  this->pitch += this->dPitch*dt;
66  while ( pitch > 2*PI )
67    pitch -= 2*PI;
68  while ( pitch < 0 )
69    pitch += 2*PI;
70 
71  myDir *= qPitch.inverse();
72 
73  qPitch = Quaternion( pitch, Vector( 1, 0, 0 ) );
74 
75  moveTowardsBox( NULL, dt );
76   
77  if ( isActive && State::getActionBox() )
78  {
79    ActionBox* box = State::getActionBox();
80    if ( box->isPointInBox( this->getAbsCoor() ) )
81    {
82      attackPlayer( box, dt );
83    }
84    else
85    {
86      moveTowardsBox( box, dt );
87    }
88  }
89 
90  myDir *= qPitch;
91 
92  this->setAbsDir( myDir );
93  this->setAbsCoor( myCoor );
94}
95
96void ActionboxEnemy::attackPlayer( ActionBox * box, float dt )
97{
98}
99
100void ActionboxEnemy::moveTowardsBox( ActionBox * box, float dt )
101{
102  //Vector targetPos = State::getPlayer()->getPlayable()->getAbsCoor();
103  static float time = 0;
104  time += dt;
105  Vector targetPos = State::getPlayer()->getPlayable()->getAbsCoor() + State::getPlayer()->getPlayable()->getAbsDir().apply(Vector(1, 0, 0))*50*(1.5 + sin(time/5));
106  Vector targetDir = targetPos - myCoor;
107 
108  Quaternion cur = myDir;
109  Quaternion rx( dt, Vector( 0, 0, 1 ) );
110 
111  Quaternion tmp1 = cur * rx;
112  Quaternion tmp2 = cur * rx.inverse();
113 
114  Quaternion dec;
115  if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(1, 0, 0)).dot(targetDir) )
116    dec = tmp1;
117  else
118    dec = tmp2;
119 
120  float dp = dec.apply( Vector(1, 0, 0) ).dot(Vector(0, 1, 0));
121  if ( dp > -0.9 && dp < 0.9 )
122  {
123    cur = dec;
124  }
125 
126  Quaternion ry( dt, cur.inverse().apply( Vector( 0, 1, 0 ) ) );
127 
128  tmp1 = cur * ry;
129  tmp2 = cur * ry.inverse();
130 
131  if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(1, 0, 0)).dot(targetDir) )
132    cur = tmp1;
133  else
134    cur = tmp2;
135 
136  myDir = cur;
137 
138  Vector fw = cur.apply( Vector(1, 0, 0) );
139 
140  this->speed += this->acceleration*dt;
141  if ( this->speed > this->maxSpeed )
142    this->speed = this->maxSpeed;
143  this->myCoor += fw*speed*dt;
144}
145
146void ActionboxEnemy::draw( ) const
147{
148#if 0
149  Vector fw = this->getAbsDir().apply( Vector( 1, 0, 0 ) );
150  fw.normalize();
151  fw = fw * 100;
152 
153  Vector mp = this->getAbsCoor();
154  Vector op = mp + fw;
155 
156  Vector targetPos = State::getPlayer()->getPlayable()->getAbsCoor();
157  Vector dv = targetPos - this->getAbsCoor();
158  dv.normalize();
159  dv *= 100;
160  dv += mp;
161 
162  Vector spUp = this->getAbsDir().inverse().apply( this->getAbsDir().apply( Vector( 0, 1, 0 ) ) );
163  spUp.normalize();
164  spUp *= 100;
165  spUp += mp;
166 
167  Vector up = fw.cross( dv );
168  up += mp;
169 
170  //PRINTF(0)("DEBUG\n");
171  //mp.debug();
172  //op.debug();
173 
174  glMatrixMode(GL_MODELVIEW);
175  glPushMatrix();
176
177  glPushAttrib(GL_ENABLE_BIT);
178
179  glDisable(GL_LIGHTING);
180  glDisable(GL_TEXTURE_2D);
181  glDisable(GL_BLEND);
182  glLineWidth(2.0);
183  glColor3f(1.0, 0.0, 0.0 );
184 
185 
186  glBegin(GL_LINE_STRIP);
187    glVertex3f(mp.x, mp.y, mp.z);
188    glVertex3f(op.x, op.y, op.z);
189  glEnd();
190 
191  glColor3f(0.0, 1.0, 0.0 );
192  glBegin(GL_LINE_STRIP);
193    glVertex3f(mp.x, mp.y, mp.z);
194    glVertex3f(dv.x, dv.y, dv.z);
195  glEnd();
196 
197  glColor3f(0.0, 0.0, 1.0 );
198  glBegin(GL_LINE_STRIP);
199    glVertex3f(mp.x, mp.y, mp.z);
200    glVertex3f(up.x, up.y, up.z);
201  glEnd();
202 
203  glColor3f(1.0, 1.0, 1.0 );
204  glBegin(GL_LINE_STRIP);
205    glVertex3f(mp.x, mp.y, mp.z);
206    glVertex3f(spUp.x, spUp.y, spUp.z);
207  glEnd();
208
209  glPopMatrix();
210#endif
211  WorldEntity::draw();
212}
Note: See TracBrowser for help on using the repository browser.