File: wentity.cpp

package info (click to toggle)
crystalspace 0.94-20020412-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 62,276 kB
  • ctags: 52,843
  • sloc: cpp: 274,783; ansic: 6,608; perl: 6,276; objc: 3,952; asm: 2,942; python: 2,354; php: 542; pascal: 530; sh: 430; makefile: 370; awk: 193
file content (243 lines) | stat: -rw-r--r-- 6,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
    Copyright (C) 2000-2001 by Jorrit Tyberghein

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "cssysdef.h"
#include "walktest/wentity.h"
#include "walktest/walktest.h"
#include "csgeom/matrix3.h"
#include "iengine/light.h"
#include "iengine/mesh.h"
#include "iengine/movable.h"

extern WalkTest* Sys;

SCF_IMPLEMENT_IBASE_EXT (csWalkEntity)
  SCF_IMPLEMENTS_INTERFACE (csWalkEntity)
SCF_IMPLEMENT_IBASE_EXT_END

//--------------------------------------------------------------------------

csDoor::csDoor (iMeshWrapper* p)
{
  is_open = false;
  transition = 0;
  tparent = p;
}

void csDoor::Activate ()
{
printf ("Activate Door!\n");
  is_open = !is_open;
  // We do 1-transition here to make sure that when we
  // activate the door while in mid-transition it will
  // just go back from that point.
  transition = 1-transition;
  // Push ourselves on to the busy list if we're not already there.
  int idx = Sys->busy_entities.Find (this);
  if (idx != -1) Sys->busy_entities.Delete (idx);
  Sys->busy_entities.Push (this);
}

void csDoor::NextFrame (float elapsed_time)
{
  if (!transition)
  {
    int idx = Sys->busy_entities.Find (this);
    if (idx != -1) Sys->busy_entities.Delete (idx);
printf ("Done opening door.\n");
    return;
  }
  transition -= elapsed_time/1000.;
  if (transition < 0) transition = 0;
  csYRotMatrix3 mat (HALF_PI * transition);
  mat.Invert ();
  tparent->GetMovable ()->SetTransform (mat);
  tparent->GetMovable ()->UpdateMove ();
}

//--------------------------------------------------------------------------


csRotatingObject::csRotatingObject (iObject* p)
{
  always = true;
  tparent = p;
  angles.Set (90, 0, 0);
  remaining = 0;
  iMeshWrapper *mw = SCF_QUERY_INTERFACE_FAST (p, iMeshWrapper);
  if (mw)
  {
    movable = mw->GetMovable ();
    mw->DecRef ();
  }
}

void csRotatingObject::Activate ()
{
  if (always) return;
  // Push ourselves on to the busy list if we're not already there.
  int idx = Sys->busy_entities.Find (this);
  if (idx != -1) Sys->busy_entities.Delete (idx);
  Sys->busy_entities.Push (this);
  remaining = 10000;
}

void csRotatingObject::NextFrame (float elapsed_time)
{
  if (remaining)
  {
    remaining -= elapsed_time;
    if (remaining <= 0)
    {
      remaining = 0;
      int idx = Sys->busy_entities.Find (this);
      if (idx != -1) Sys->busy_entities.Delete (idx);
    }
  }
  else if (!always) return;

  float trans = (TWO_PI / 360.) * (elapsed_time / 1000.);
  csXRotMatrix3 matx (angles.x * trans);
  csYRotMatrix3 maty (angles.y * trans);
  csZRotMatrix3 matz (angles.z * trans);
  csMatrix3 mat = matz * maty * matx;
  movable->Transform (mat);
  movable->UpdateMove ();
}

//--------------------------------------------------------------------------

csLightObject::csLightObject (iLight* p)
{
  light = p;
  act_time = 1000;
  cur_time = 0;
}

void csLightObject::Activate ()
{
  // Push ourselves on to the busy list if we're not already there.
  int idx = Sys->busy_entities.Find (this);
  if (idx != -1) Sys->busy_entities.Delete (idx);
  Sys->busy_entities.Push (this);
  cur_time = act_time;
}

void csLightObject::NextFrame (float elapsed_time)
{
  if (cur_time <= 0) return;
  cur_time -= elapsed_time;
  if (cur_time <= 0)
  {
    cur_time = 0;
    int idx = Sys->busy_entities.Find (this);
    if (idx != -1) Sys->busy_entities.Delete (idx);
  }

  csColor s_color (start_color);
  csColor e_color (end_color);
  s_color *= cur_time/act_time;
  e_color *= (act_time-cur_time)/act_time;
  light->SetColor (s_color+e_color);
}

//--------------------------------------------------------------------------

struct AnimPortalCallback : public iPortalCallback
{
  AnimPortalCallback ();
  csAnimatedPortal* animportal;
  SCF_DECLARE_IBASE;
  virtual bool Traverse (iPortal* portal, iBase* context);
};

SCF_IMPLEMENT_IBASE (AnimPortalCallback)
  SCF_IMPLEMENTS_INTERFACE (iPortalCallback)
SCF_IMPLEMENT_IBASE_END

AnimPortalCallback::AnimPortalCallback ()
{
  SCF_CONSTRUCT_IBASE (NULL);
}

bool AnimPortalCallback::Traverse (iPortal*, iBase* )
{
  animportal->visible = true;
  return true;
}

csAnimatedPortal::csAnimatedPortal (iPortal* p,
	int xyz, float max_angle, float speed)
{
  portal = p;
  AnimPortalCallback* cb = new AnimPortalCallback ();
  cb->animportal = this;
  portal->SetPortalCallback (cb);
  cb->DecRef ();
  csAnimatedPortal::xyz = xyz;
  csAnimatedPortal::max_angle = max_angle;
  csAnimatedPortal::speed = speed;
  orig_trans = portal->GetWarp ();
  cur_angle = 0;
  cur_dir = 1;
  visible = false;
}

void csAnimatedPortal::Activate ()
{
  // Push ourselves on to the busy list if we're not already there.
  int idx = Sys->busy_entities.Find (this);
  if (idx != -1) Sys->busy_entities.Delete (idx);
  Sys->busy_entities.Push (this);
}

void csAnimatedPortal::NextFrame (float elapsed_time)
{
  if (!visible) return;
  visible = false;

  if (cur_dir == 1)
  {
    cur_angle += elapsed_time/speed;
    if (cur_angle > max_angle) { cur_angle = max_angle; cur_dir = -1; }
  }
  else
  {
    cur_angle -= elapsed_time/speed;
    if (cur_angle < -max_angle) { cur_angle = -max_angle; cur_dir = 1; }
  }

  csReversibleTransform trans = orig_trans;
  switch (xyz)
  {
    case 1:
      trans *= csTransform (csXRotMatrix3 (cur_angle), csVector3 (0));
      break;
    case 2:
      trans *= csTransform (csYRotMatrix3 (cur_angle), csVector3 (0));
      break;
    case 3:
      trans *= csTransform (csZRotMatrix3 (cur_angle), csVector3 (0));
      break;
  }
  portal->SetWarp (trans);
}


//--------------------------------------------------------------------------