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 /** Definiton of a rope.
24  * @file    HCRope.h
25  * @author  Juan Carlos Seijo P�rez
26  * @date    05/06/2004
27  * @version 0.0.1 -  05/06/2004 - First version.
28  */
29 
30 #include <HCRope.h>
31 
Init(float p,s32 a,u32 l,HCTheme & theme)32 bool HCRope::Init(float p, s32 a, u32 l, HCTheme &theme)
33 {
34 	// Creates references of the images and places the rope in its original position
35 	top.Ref(theme.Rope(subtype)[HCRDT_TOP]);
36 	top.Pos(pos.x, pos.y);
37 
38 	period = p >= 1.0f ? p : 1.0f;
39 	w = 6.2831853/period;
40 	length = l > 0 ? l : 5;
41 	amplitude = abs(a) < length * theme.Rope(subtype)[HCRDT_MIDDLE].Height() ? abs(a) : length * theme.Rope(subtype)[HCRDT_MIDDLE].Height();
42 
43 	s32 i;
44 	float y;
45 
46 	JDELETE_ARRAY(middle);
47 	middle = new JImage [length];
48 
49 	if (middle == 0)
50 		return false;
51 
52 	for (i = 0; i < length; ++i)
53 	{
54 		middle[i].Ref(theme.Rope(subtype)[HCRDT_MIDDLE]);
55 		y = top.Y() + top.Height() + (i * middle[i].Height());
56 		middle[i].Pos(top.X() + (top.Width()/2) - (middle[i].Width()/2), pos.y);
57 	}
58 
59 	edge.Ref(theme.Rope(subtype)[HCRDT_EDGE]);
60 	y = top.Y() + top.Height() + (i * middle[0].Height());
61 	edge.Pos(top.X() + (top.Width()/2) - (edge.Width()/2), pos.y);
62 
63 	// 25 fps resolution
64 	timer.Start(40);
65 
66 	return true;
67 }
68 
Draw()69 void HCRope::Draw()
70 {
71 	top.Draw();
72 
73 	for (s32 i = 0; i < length; ++i)
74 	{
75 		middle[i].Draw();
76 	}
77 
78 	edge.Draw();
79 }
80 
Update()81 s32 HCRope::Update()
82 {
83 	float t = float(timer.TotalLap())/1000.0f;
84 
85 	edge.X((top.X() + top.Width()/2) - (edge.Width()/2) + (amplitude * cos(w * t)));
86 
87 	float alpha = asin((float(amplitude) * cos(w * t))/float(length * middle[0].Height()));
88 	float newY = length * middle[0].Height() * cos(alpha);
89 
90 	edge.Y(top.Y() + top.Height() + newY);
91 
92 	float dx = ((edge.X() + edge.Width()/2) - (top.X() + top.Width()/2))/float(length);
93 	float dy = ((edge.Y() + edge.Height()/2) - (top.Y() + top.Height()))/float(length);
94 
95 	for (s32 i = 0; i < length; ++i)
96 	{
97 		middle[i].X((top.X() + top.Width()/2) + (i * dx) - (middle[i].Width()/2));
98 		middle[i].Y((top.Y() + top.Height()) + (i * dy));
99 	}
100 
101 	direction = (s32)(10.0f * dx);
102 
103 	return 0;
104 }
105 
Pos(float x,float y)106 void HCRope::Pos(float x, float y)
107 {
108 	float dx, dy;
109 	dx = x - pos.x;
110 	dy = y - pos.y;
111 
112 	pos.x = x;
113 	pos.y = y;
114 
115 	for (s32 i = 0; i < length; ++i)
116 	{
117 		middle[i].X(middle[i].X() + dx);
118 		middle[i].Y(middle[i].Y() + dy);
119 	}
120 
121 	edge.X(edge.X() + dx);
122 	edge.Y(edge.Y() + dy);
123 
124 	top.X(x);
125 	top.Y(y);
126 }
127 
Load(JRW & file)128 u32 HCRope::Load(JRW &file)
129 {
130 	if (0 == file.ReadLE32((u32 *)&period) ||
131 			0 == file.ReadLE32(&amplitude) ||
132 			0 == file.ReadLE32(&length) ||
133 			0 == file.ReadLE32(&subtype) ||
134 			0 == file.ReadLE32((u32 *)&pos.x) ||
135 			0 == file.ReadLE32((u32 *)&pos.y))
136 	{
137 		fprintf(stderr, "Error reading rope parameters.\n");
138 
139 		return 1;
140 	}
141 
142 	return 0;
143 }
144 
Load(JRW & file,HCTheme & theme)145 u32 HCRope::Load(JRW &file, HCTheme &theme)
146 {
147 	if (0 != Load(file))
148 	{
149 		return 1;
150 	}
151 
152 	if (!Init(period, amplitude, length, theme))
153 	{
154 		return 2;
155 	}
156 
157 	Pos(pos.x, pos.y);
158 
159 	return 0;
160 }
161 
Save(JRW & file)162 u32 HCRope::Save(JRW &file)
163 {
164 	if (0 == file.WriteLE32((u32 *)&period) ||
165 			0 == file.WriteLE32(&amplitude) ||
166 			0 == file.WriteLE32(&length) ||
167 			0 == file.WriteLE32(&subtype) ||
168 			0 == file.WriteLE32((u32 *)&pos.x) ||
169 			0 == file.WriteLE32((u32 *)&pos.y))
170 	{
171 		fprintf(stderr, "Error writing rope parameters.\n");
172 
173 		return 1;
174 	}
175 
176 	return 0;
177 }
178 
~HCRope()179 HCRope::~HCRope()
180 {
181 	JDELETE_ARRAY(middle);
182 }
183