1 /*
2  * Holotz's Castle
3  * Copyright (C) 2004 Juan Carlos Seijo P�rez
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc., 59
17  * Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Juan Carlos Seijo P�rez
20  * jacob@mainreactor.net
21  */
22 
23 /** Defines an enemy ball.
24  * @file    HCEnemyBall.cpp
25  * @author  Juan Carlos Seijo P�rez
26  * @date    27/05/2004
27  * @version 0.0.1 - 27/05/2004 - First version.
28  */
29 
30 #include <HCEnemyBall.h>
31 
HCEnemyBall()32 HCEnemyBall::HCEnemyBall() : HCEnemy(HCENEMYTYPE_BALL)
33 {
34 	// Depende de param1 o param2
35 	actions = (param1 % 2  ? HCCA_LEFT : HCCA_RIGHT);
36 	v.x = (float)(param1 % 2 ? -(param1 + 1) : (param1 + 1));
37 	vMax.x = v.x;
38 	state = HCCS_LEFT;
39 }
40 
UpdateCollisions()41 void HCEnemyBall::UpdateCollisions()
42 {
43 	s32 row = map->ToRow((s32)pos.y), col = map->ToCol((s32)pos.x), newCol = map->ToCol((s32)(pos.x + v.x));
44 
45 	// Side collisions
46 	switch (map->Cell(row, newCol)->Type())
47 	{
48 	case HCCELLTYPE_FLOOR:
49 	case HCCELLTYPE_CONTFLOOR:
50 		v.x = -v.x;
51 		if (actions & HCCA_RIGHT)
52 		{
53 			actions &= ~HCCA_RIGHT;
54 			actions |= HCCA_LEFT;
55 		}
56 		else
57 		{
58 			actions &= ~HCCA_LEFT;
59 			actions |= HCCA_RIGHT;
60 		}
61 		break;
62 
63 	case HCCELLTYPE_BREAK:
64 		if (((HCBreak *)map->Cell(row, newCol))->State() == HCBREAKSTATE_NORMAL)
65 		{
66 			v.x = -v.x;
67 			if (actions & HCCA_RIGHT)
68 			{
69 				actions &= ~HCCA_RIGHT;
70 				actions |= HCCA_LEFT;
71 			}
72 			else
73 			{
74 				actions &= ~HCCA_LEFT;
75 				actions |= HCCA_RIGHT;
76 			}
77 		}
78 		break;
79 
80 	default:
81 		col = newCol;
82 		break;
83 	}
84 
85 	s32 newRow = map->ToRow((s32)(pos.y + v.y));
86 
87 	// Updown collisions
88 	switch (map->Cell(newRow, col)->Type())
89 	{
90 	case HCCELLTYPE_LADDER:
91 	case HCCELLTYPE_BAR:
92 
93 		// New cell is a ladder or a bar
94 		switch (state)
95 		{
96 		case HCCS_UP:
97 		case HCCS_DOWN:
98 		case HCCS_SLIDE:
99 			// In this states do nothing
100 			break;
101 
102 		case HCCS_JUMPLEFT:
103 		case HCCS_JUMPRIGHT:
104 		case HCCS_JUMP:
105 			if (newRow > row && map->Cell(row, col)->Type() == HCCELLTYPE_BLANK)
106 			{
107 				// Falls over a ladder or bar, must stop
108 				v.y = 0.0f;
109 				if (actions & HCCA_RIGHT)
110 				{
111 					actions &= ~HCCA_RIGHT;
112 					actions |= HCCA_LEFT;
113 				}
114 				else
115 				{
116 					actions &= ~HCCA_LEFT;
117 					actions |= HCCA_RIGHT;
118 				}
119 			}
120 			break;
121 
122 		default:
123 			// In any other state, stops the character
124 			v.y = 0.0f;
125 			break;
126 		}
127 		break;
128 
129 	default:
130 		if ((map->Cell(newRow, col)->Actions() & HCACTION_FALL) == 0)
131 		{
132 			v.y = 0.0f;
133 
134 			switch (state)
135 			{
136 				// If jumping, must stop
137 			case HCCS_JUMPLEFT:
138 			case HCCS_JUMPRIGHT:
139 			case HCCS_JUMP:
140 				if (newRow > row)
141 				{
142 					actions &= ~HCCA_JUMP;
143 				}
144 				break;
145 
146 			case HCCS_DOWN:
147 			case HCCS_SLIDE:
148 				actions &= ~HCCA_DOWN;
149 				break;
150 
151 			default:
152 				break;
153 			}
154 		}
155 		else
156 		{
157 			if (newRow > row &&
158 					state != HCCS_JUMP &&
159 					state != HCCS_JUMPLEFT &&
160 					state != HCCS_JUMPRIGHT)
161 			{
162 				actions &= ~HCCA_JUMP;
163 			}
164 		}
165 		break;
166 	}
167 }
168