1 /**
2  * @file controller_magnetic_eyes.cc
3  * @brief Magectic eyes controller
4  * @created 2004-09-17
5  * @date 2007-10-21
6  * @copyright 1991-2014 TLK Games
7  * @author Bruno Ethvignot
8  * @version $Revision: 24 $
9  */
10 /*
11  * copyright (c) 1991-2014 TLK Games all rights reserved
12  * $Id: controller_magnetic_eyes.cc 24 2014-09-28 15:30:04Z bruno.ethvignot@gmail.com $
13  *
14  * TecnoballZ is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * TecnoballZ is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  * MA  02110-1301, USA.
28  */
29 #include "../include/controller_magnetic_eyes.h"
30 #include "../include/handler_keyboard.h"
31 #include "../include/handler_resources.h"
32 
33 /**
34  * Create the magectic eyes controller
35  */
controller_magnetic_eyes()36 controller_magnetic_eyes::controller_magnetic_eyes ()
37 {
38   littleInit ();
39   max_of_sprites = 3;
40   sprites_have_shades = true;
41   sprite_type_id = sprite_object::MAGNETIC_EYE;
42 }
43 
44 /**
45  * Release the magectic eyes controller
46  */
~controller_magnetic_eyes()47 controller_magnetic_eyes::~controller_magnetic_eyes ()
48 {
49   release_sprites_list ();
50 }
51 
52 /**
53  * Enable a new eye
54  * @return true if an eye was activated
55  */
create_eye()56 bool controller_magnetic_eyes::create_eye ()
57 {
58   for (Uint32 i = 0; i < max_of_sprites; i++)
59     {
60       sprite_eye *
61       eye = sprites_list[i];
62       if (eye->is_enabled)
63         {
64           continue;
65         }
66       eye->is_enabled = true;
67       return true;
68     }
69   return false;
70 }
71 
72 /**
73  * Create a initialize eyes sprites
74  */
75 void
create_eyes_list()76 controller_magnetic_eyes::create_eyes_list ()
77 {
78   Sint32 res = resolution;
79   create_sprites_list ();
80   Sint32 random = random_counter;
81   for (Uint32 i = 0; i < max_of_sprites; i++)
82     {
83       sprite_eye *eye = sprites_list[i];
84       eye->centerPosx = x_coordinates[random & 31] * res;
85 #if __WORDSIZE == 64
86       random += (long) eye;
87 #else
88       random += (intptr_t) eye;
89 #endif
90       eye->centerPosy = y_coordinates[random & 31] * res;
91       random += keyboard->get_mouse_y ();
92       eye->finishPosx = x_coordinates[random & 31] * res;
93       random += keyboard->get_mouse_x ();
94       eye->finishPosy = y_coordinates[random & 31] * res;
95       random++;
96       eye->radius_360 = 0;
97     }
98   /* value used for the collisions with balls */
99   hypotenuse = sprites_list[0]->collision_width - res * 2;
100   center_x = hypotenuse / 2;
101   center_y = center_x;
102   hypotenuse = hypotenuse * hypotenuse;
103 }
104 
105 /**
106  * Move all eyes
107  */
108 void
move()109 controller_magnetic_eyes::move ()
110 {
111   Sint32 random = random_counter;
112   Sint32 res = resolution;
113   for (Uint32 i = 0; i < max_of_sprites; i++)
114     {
115       sprite_eye *eye = sprites_list[i];
116       eye->play_animation_loop ();
117 
118       /* verify if center coordinates arrived at destination */
119       Sint32 j = 3 * res;
120       if (eye->centerPosx > eye->finishPosx - j &&
121           eye->centerPosx < eye->finishPosx + j &&
122           eye->centerPosy > eye->finishPosy - j &&
123           eye->centerPosy < eye->finishPosy + j)
124         {
125           random += keyboard->get_mouse_y ();
126           eye->finishPosx = x_coordinates[random & 31] * res;
127           random += keyboard->get_mouse_x ();
128           eye->finishPosy = y_coordinates[random & 31] * res;
129         }
130 
131       /* move center */
132       Sint32 inc_x = res;
133       Sint32 inc_y = res;
134       Sint32 delta_x = eye->finishPosx - eye->centerPosx;
135       if (delta_x < 0)
136         {
137           delta_x = -delta_x;
138           inc_x = -res;
139         }
140       Sint32 delta_y = eye->finishPosy - eye->centerPosy;
141       if (delta_y < 0)
142         {
143           delta_y = -delta_y;
144           inc_y = -res;
145         }
146       Sint32 hflag = 0;
147       if (delta_y > delta_x)
148         {
149           j = delta_y;
150           delta_y = delta_x;
151           delta_x = j;
152           hflag = 1;
153         }
154       Sint32 value = delta_y * 2 - delta_x;
155       delta_x *= 2;
156       j = delta_x - 1;
157       do
158         {
159           if (hflag)
160             {
161               eye->centerPosy += inc_y;
162             }
163           else
164             {
165               eye->centerPosx += inc_x;
166             }
167           value -= delta_x;
168           if (value < 0)
169             {
170               if (!hflag)
171                 {
172                   eye->centerPosy += inc_y;
173                 }
174               else
175                 {
176                   eye->centerPosx += inc_x;
177                 }
178               break;
179             }
180           j--;
181         }
182       while (j >= 0);
183 
184 
185       /* move circle */
186       eye->radius_360 = (eye->radius_360 + 4) % 360;
187       Sint32 x =
188         (handler_resources::zesinus360[eye->radius_360] * 10 * res) >> 7;
189       Sint32 y =
190         (handler_resources::cosinus360[eye->radius_360] * 10 * res) >> 7;
191       eye->x_coord = eye->centerPosx + x + (15 * res);
192       eye->y_coord = eye->centerPosy + y + (15 * res);
193       random += 4;
194     }
195 }
196 const Uint16
197 controller_magnetic_eyes::x_coordinates[32] =
198   {
199     40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 92, 96, 100, 105, 111, 115,
200     118, 120, 122, 128, 130, 132, 135, 140, 144, 146, 150, 152, 160, 164, 166,
201     170
202   };
203 const
204 Uint16
205 controller_magnetic_eyes::y_coordinates[32] =
206   {
207     40, 44, 56, 58, 60, 62, 70, 78, 80, 82, 85, 90, 92, 94, 96, 100,
208     101, 120, 122, 124, 130, 138, 144, 146, 148, 150, 152, 153, 154, 155, 160,
209     170
210   };
211