Rebuild autotool system
[construo.git] / rect_collider.cxx
blob75d7d792fa3c980ae311316a167c2aaf614964bb
1 // $Id: rect_collider.cxx,v 1.10 2003/07/26 11:18:47 grumbel Exp $
2 //
3 // Construo - A wire-frame construction gamee
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <math.h>
21 #include "colors.hxx"
22 #include "particle_factory.hxx"
23 #include "controller.hxx"
24 #include "rect_collider.hxx"
25 #include "construo_error.hxx"
27 Collider*
28 RectCollider::duplicate() const
30 return new RectCollider(x1, y1, x2, y2);
33 RectCollider::RectCollider (lisp_object_t* cursor)
35 Vector2d pos1, pos2;
37 LispReader reader(cursor);
38 if (reader.read_vector("pos1", &pos1) == false
39 || reader.read_vector("pos2", &pos2) == false)
41 throw ConstruoError("RectCollider entry incomplete");
44 x1 = pos1.x;
45 y1 = pos1.y;
46 x2 = pos2.x;
47 y2 = pos2.y;
50 RectCollider::RectCollider (float x1_, float y1_, float x2_, float y2_)
51 : x1 (Math::min(x1_, x2_)),
52 y1 (Math::min(y1_, y2_)),
53 x2 (Math::max(x1_, x2_)),
54 y2 (Math::max(y1_, y2_))
58 bool
59 RectCollider::is_at (const Vector2d& pos)
61 return (x1 <= pos.x && x2 > pos.x
62 && y1 <= pos.y && y2 > pos.y);
65 Vector2d
66 RectCollider::get_pos()
68 return Vector2d ((x1 + x2)/2.0f,
69 (y1 + y2)/2.0f);
72 void
73 RectCollider::set_pos(const Vector2d& pos)
75 Vector2d center = get_pos();
76 x1 = x1 - center.x + pos.x;
77 x2 = x2 - center.x + pos.x;
78 y1 = y1 - center.y + pos.y;
79 y2 = y2 - center.y + pos.y;
82 void
83 RectCollider::bounce ()
85 ParticleFactory* particle_mgr = Controller::instance()->get_world()->get_particle_mgr();
87 float damp = 0.8;
88 for (ParticleFactory::ParticleIter i = particle_mgr->begin(); i != particle_mgr->end (); ++i)
90 Vector2d& pos = (*i)->pos;
91 Vector2d& velocity = (*i)->velocity;
93 if (pos.x > x1 && pos.x < x2
94 && pos.y > y1 && pos.y < y2)
96 float left_dist = pos.x - x1;
97 float right_dist = x2 - pos.x;
99 float top_dist = pos.y - y1;
100 float bottom_dist = y2 - pos.y;
102 if (left_dist < right_dist
103 && left_dist < top_dist
104 && left_dist < bottom_dist)
106 velocity.x = -fabs(velocity.x);
107 pos.x = x1;
109 else if (right_dist < left_dist
110 && right_dist < top_dist
111 && right_dist < bottom_dist)
113 velocity.x = fabs(velocity.x);
114 pos.x = x2;
116 else if (top_dist < left_dist
117 && top_dist < right_dist
118 && top_dist < bottom_dist)
120 velocity.y = -fabs(velocity.y);
121 pos.y = y1;
123 else
125 velocity.y = fabs(velocity.y);
126 pos.y = y2;
128 velocity *= damp;
133 void
134 RectCollider::draw (GraphicContext* gc)
136 //std::cout << "Drawing collider" << std::endl;
137 gc->draw_fill_rect (x1, y1, x2, y2, Colors::rect_collider_bg);
138 gc->draw_rect (x1, y1, x2, y2, Colors::rect_collider_fg);
141 void
142 RectCollider::draw_highlight (GraphicContext* gc)
144 //gc->draw_fill_rect (x1, y1, x2, y2, Colors::rect_collider_bg);
145 gc->draw_rect (x1, y1, x2, y2, Colors::selection_rect);
148 BoundingBox
149 RectCollider::get_bounding_box() const
151 return BoundingBox(x1, y1, x2, y2);
154 lisp_object_t*
155 RectCollider::serialize()
157 LispWriter obj ("rect");
158 obj.write_vector ("pos1", Vector2d(x1, y1));
159 obj.write_vector ("pos2", Vector2d(x2, y2));
160 return obj.create_lisp ();
163 /* EOF */