source: golgotha/src/golg/objs/shrapnel.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 5.2 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "g1_object.hh"
10#include "math/num_type.hh"
11#include "objs/shrapnel.hh"
12#include "objs/explosion1.hh"
13#include "saver.hh"
14#include "map.hh"
15#include "map_man.hh"
16#include "math/angle.hh"
17#include "math/trig.hh"
18#include "g1_rand.hh"
19#include "math/random.hh"
20#include "resources.hh"
21#include "objs/particle_emitter.hh"
22#include "g1_render.hh"
23#include "object_definer.hh"
24#include "draw_context.hh"
25#include "r1_clip.hh"
26#include "r1_api.hh"
27
28static g1_object_type particle_emitter_type;
29static r1_texture_ref gradient("fire_gradient");
30
31enum { DATA_VERSION=2 };
32
33const int MAX_SHRAPNEL_TIME=5;
34 
35void g1_shrapnel_init()
36{
37  particle_emitter_type = g1_get_object_type("particle_emitter");
38}
39
40g1_object_definer<g1_shrapnel_class>
41g1_shrapnel_def("shrapnel", 0, g1_shrapnel_init);
42
43g1_shrapnel_class::g1_shrapnel_class(g1_object_type id,
44                                     g1_loader_class *fp)
45  : g1_object_class(id,fp)
46
47 
48  w16 ver,data_size;
49  w32 i;
50
51  num_shrapnel_pieces = 0;
52  shrapnel_time = 0;
53  x = -1;
54}
55
56void g1_shrapnel_class::save(g1_saver_class *fp)
57{
58  // save data associated with base classes
59  g1_object_class::save(fp);
60 
61  fp->start_version(DATA_VERSION);
62  fp->end_version();
63}
64
65i4_float t_grad_width=0.2;
66
67void g1_shrapnel_class::draw(g1_draw_context_class *context)
68{   
69  w32 i;
70 
71  shrapnel_piece *p=shrapnel_pieces;
72  p = shrapnel_pieces;
73
74  i4_3d_vector i_pos;
75
76  i4_transform_class trans = *context->transform;
77  r1_vert      v[3];
78  i4_3d_vector ip,op;
79  i4_float offs = i4_float(shrapnel_time)*(1.0-t_grad_width)/MAX_SHRAPNEL_TIME;
80
81  v[0].a=v[1].a=v[2].a = 1;
82  v[0].r=v[0].g=v[0].b = 1;
83  v[0].s=offs;
84  v[0].t=0;
85  v[1].r=v[1].g=v[1].b = 1;
86  v[1].s=offs+t_grad_width;
87  v[1].t=0.5;
88  v[2].r=v[2].g=v[2].b = 1;
89  v[2].s=offs;
90  v[2].t=1.0;
91
92  trans.mult_translate(x,y,h);
93
94  g1_render.r_api->use_texture(gradient.get(), 32, 0);
95
96  for (i=0; i<num_shrapnel_pieces; i++,p++)
97  {       
98    i4_3d_vector d(p->position);
99    d -= p->lposition;
100    d *= 1.0 - g1_render.frame_ratio;
101    ip = p->position;
102    op = p->lposition;
103    ip -= d;
104    op -= d;
105
106    trans.transform(ip, *v[0].point());
107    trans.transform(op, *v[1].point());
108
109    if (!r1_calc_outcode(&v[0]) && !r1_calc_outcode(&v[1]))
110    {
111      v[0].v.x *= g1_render.scale_x;
112      v[0].v.y *= g1_render.scale_y;
113      v[0].w = 1/v[0].v.z;
114      v[0].px = v[0].v.x * v[0].w;
115      v[0].py = v[0].v.y * v[0].w;
116
117      v[1].v.x *= g1_render.scale_x;
118      v[1].v.y *= g1_render.scale_y;
119      v[1].w = 1/v[1].v.z;
120      v[1].px = v[1].v.x * v[1].w;
121      v[1].py = v[1].v.y * v[1].w;
122
123      const i4_float SIZE=0.01;
124
125      v[2].v = v[0].v;
126      v[2].w = v[0].w;
127
128      i4_float dir = ((v[0].px>v[1].px) ^ (v[0].py>v[1].py))? 1 : -1;
129
130      v[2].px = v[0].px-i4_float_rand()*SIZE*v[0].w;
131      v[2].py = v[0].py-i4_float_rand()*SIZE*v[0].w*dir;
132      v[0].px = v[0].px+i4_float_rand()*SIZE*v[0].w;
133      v[0].py = v[0].py+i4_float_rand()*SIZE*v[0].w*dir;
134     
135      i4_bool ok=i4_T;
136      for (int j=0; j<3 && ok; j++)
137      {
138        if (v[j].px<=-1.0 || v[j].px>=1.0 || v[j].py<=-1.0 || v[j].py>=1.0)
139          ok = i4_F;
140        else
141        {
142          v[j].px = v[j].px*g1_render.center_x + g1_render.center_x;
143          v[j].py = v[j].py*g1_render.center_y + g1_render.center_y;
144        }
145      }
146      if (ok)
147        g1_render.r_api->render_poly(3,v);
148    }
149  }   
150}
151
152void g1_shrapnel_class::think()
153{   
154  w32 i;
155
156  if (shrapnel_time<MAX_SHRAPNEL_TIME)
157  {
158    shrapnel_time++;
159    shrapnel_piece *p = shrapnel_pieces;
160    for (i=0;i<num_shrapnel_pieces;i++,p++)
161    {     
162      p->lposition = p->position;
163      p->position += p->velocity;
164      p->velocity.z -= g1_resources.gravity;
165    }
166    request_think();
167  }
168  else
169  {
170    unoccupy_location();
171    request_remove(); 
172  }
173}
174
175static r1_texture_ref explosion2("explosions2");
176
177void g1_shrapnel_class::setup(i4_float sx, i4_float sy, i4_float sz,
178                              w32 _num_pieces, int type)
179{
180  x=lx=sx;
181  y=ly=sy;
182  h=lh=sz;
183
184  if (!occupy_location())
185    return;
186
187  shrapnel_time = 0;
188  num_shrapnel_pieces = _num_pieces;
189
190  if (num_shrapnel_pieces > MAX_SHRAPNEL_PIECES)
191    num_shrapnel_pieces=MAX_SHRAPNEL_PIECES;
192 
193  shrapnel_piece *p = shrapnel_pieces;
194   
195  for (int i=0;i<num_shrapnel_pieces;i++,p++)
196  {
197    i4_float f=0.2;
198    i4_float
199      rx = i4_float_rand() * f - f/2.0,
200      ry = i4_float_rand() * f - f/2.0,
201      rz = i4_float_rand() * f;  // - f/2.0;
202   
203    p->type=g1_rand(39)&3;
204
205    p->lposition = p->position = i4_3d_vector(0,0,0);
206    p->velocity = i4_3d_vector(rx,ry,rz);
207  }
208
209  request_think();
210}
Note: See TracBrowser for help on using the repository browser.