Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10655 was 10655, checked in by rennerc, 18 years ago

ActionboxEnemy looks at Player :D

File size: 3.7 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  //TODO init to zero for non-debug
41  this->pitch = 0.0f;
42  this->dPitch = PI;
43 
44  if ( root )
45    this->loadParams( root );
46}
47
48ActionboxEnemy::~ActionboxEnemy()
49{
50}
51
52void ActionboxEnemy::loadParams(const TiXmlElement* root)
53{
54  WorldEntity::loadParams( root );
55}
56
57void ActionboxEnemy::tick( float dt )
58{
59  this->pitch += this->dPitch*dt;
60 
61  moveTowardsBox( NULL, dt );
62  return;
63 
64  if ( isActive && State::getActionBox() )
65  {
66    ActionBox* box = State::getActionBox();
67    if ( box->isPointInBox( this->getAbsCoor() ) )
68    {
69      attackPlayer( box, dt );
70    }
71    else
72    {
73      moveTowardsBox( box, dt );
74    }
75  }
76}
77
78void ActionboxEnemy::attackPlayer( ActionBox * box, float dt )
79{
80}
81
82void ActionboxEnemy::moveTowardsBox( ActionBox * box, float dt )
83{
84  Vector targetPos = State::getPlayer()->getPlayable()->getAbsCoor();
85  Vector targetDir = targetPos - this->getAbsCoor();
86 
87 
88  Quaternion cur = this->getAbsDir();
89  Quaternion rx( 5*dt, Vector( 0, 0, 1 ) );
90 
91  Quaternion tmp1 = cur * rx;
92  Quaternion tmp2 = cur * rx.inverse();
93 
94  if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(1, 0, 0)).dot(targetDir) )
95    cur = tmp1;
96  else
97    cur = tmp2;
98 
99  Quaternion ry( 5*dt, cur.inverse().apply( Vector( 0, 1, 0 ) ) );
100 
101  tmp1 = cur * ry;
102  tmp2 = cur * ry.inverse();
103 
104  if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(1, 0, 0)).dot(targetDir) )
105    cur = tmp1;
106  else
107    cur = tmp2;
108 
109  this->setAbsDir( cur ); 
110}
111
112void ActionboxEnemy::draw( ) const
113{
114  Vector fw = this->getAbsDir().apply( Vector( 1, 0, 0 ) );
115  fw.normalize();
116  fw = fw * 100;
117 
118  Vector mp = this->getAbsCoor();
119  Vector op = mp + fw;
120 
121  Vector targetPos = State::getPlayer()->getPlayable()->getAbsCoor();
122  Vector dv = targetPos - this->getAbsCoor();
123  dv.normalize();
124  dv *= 100;
125  dv += mp;
126 
127  Vector spUp = this->getAbsDir().inverse().apply( this->getAbsDir().apply( Vector( 0, 1, 0 ) ) );
128  spUp.normalize();
129  spUp *= 100;
130  spUp += mp;
131 
132  Vector up = fw.cross( dv );
133  up += mp;
134 
135  //PRINTF(0)("DEBUG\n");
136  //mp.debug();
137  //op.debug();
138 
139  glMatrixMode(GL_MODELVIEW);
140  glPushMatrix();
141
142  glPushAttrib(GL_ENABLE_BIT);
143
144  glDisable(GL_LIGHTING);
145  glDisable(GL_TEXTURE_2D);
146  glDisable(GL_BLEND);
147  glLineWidth(2.0);
148  glColor3f(1.0, 0.0, 0.0 );
149 
150 
151  glBegin(GL_LINE_STRIP);
152    glVertex3f(mp.x, mp.y, mp.z);
153    glVertex3f(op.x, op.y, op.z);
154  glEnd();
155 
156  glColor3f(0.0, 1.0, 0.0 );
157  glBegin(GL_LINE_STRIP);
158    glVertex3f(mp.x, mp.y, mp.z);
159    glVertex3f(dv.x, dv.y, dv.z);
160  glEnd();
161 
162  glColor3f(0.0, 0.0, 1.0 );
163  glBegin(GL_LINE_STRIP);
164    glVertex3f(mp.x, mp.y, mp.z);
165    glVertex3f(up.x, up.y, up.z);
166  glEnd();
167 
168  glColor3f(1.0, 1.0, 1.0 );
169  glBegin(GL_LINE_STRIP);
170    glVertex3f(mp.x, mp.y, mp.z);
171    glVertex3f(spUp.x, spUp.y, spUp.z);
172  glEnd();
173
174  glPopMatrix();
175 
176  WorldEntity::draw();
177}
Note: See TracBrowser for help on using the repository browser.