1 | // |
---|
2 | // Orbital |
---|
3 | // |
---|
4 | // Copyright: (c) 2009-2012 Cédric Lecacheur <jordx@free.fr> |
---|
5 | // (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com> |
---|
6 | // (c) 2012 Sam Hocevar <sam@hocevar.net> |
---|
7 | // |
---|
8 | |
---|
9 | /* FIXME: this file is pure crap; it's only a test. */ |
---|
10 | |
---|
11 | #if !defined __PLAYER_H__ |
---|
12 | #define __PLAYER_H__ |
---|
13 | |
---|
14 | class Player : public WorldEntity |
---|
15 | { |
---|
16 | public: |
---|
17 | Player(int type) |
---|
18 | : m_type(type), |
---|
19 | m_stick(0), |
---|
20 | m_damped_stick(0), |
---|
21 | m_move_rotation(0), |
---|
22 | m_exhaust_rotation(0), |
---|
23 | move_rotation_timer(0), |
---|
24 | m_ready(false) |
---|
25 | { |
---|
26 | if (type == 0) |
---|
27 | { |
---|
28 | /* Red Mesh */ |
---|
29 | m_ship_mesh.Compile("[sc#c32 afcb3 6 7 .4 t0 0 7" |
---|
30 | " sc#fff afcb3 4 4 .4 t4 0 -4 mx]" |
---|
31 | "[sc#c32 afcb3 6 5 .4 sc#fff afcb2 3 9 .4]" |
---|
32 | "[sc#fff ac4 15 .2 .6 1 1 tz-2" |
---|
33 | " ac4 15 .2 .6 1 1 rx90 t0 -2 -7]"); |
---|
34 | |
---|
35 | m_exhaust_mesh.Compile("oifds9898432890fds"); |
---|
36 | m_exhaust_mesh.Compile("[sc#0ff scb#000 ac5 15 0 1.5 0 1" |
---|
37 | " ac7 35 1.1 4 0 1 rx90 tz27]"); |
---|
38 | |
---|
39 | m_option_mesh.Compile("sc#c32[afcb5 1 3 0.6]" |
---|
40 | "sc#fff[afcb1 5 3 0.6 tz-1]"); |
---|
41 | } |
---|
42 | else |
---|
43 | { |
---|
44 | /* Blue Mesh */ |
---|
45 | m_ship_mesh.Compile("[sc#fff scb#fff ac4 15 .2 .6 1 1 t0 0 -4" |
---|
46 | " ac4 1.5 .2 .6 1 1 rx90 t1.3 -2 -6" |
---|
47 | " afcb3 6 5 .4 t0 0 -7" |
---|
48 | " sc#229 afcb3 4 7 .4 t4 0 0 mx]" |
---|
49 | "[afcb3 6 5 .4 sc#fff afcb2 3 9 .4]"); |
---|
50 | |
---|
51 | m_exhaust_mesh.Compile("[sc#0ff scb#000 ac5 10 0 1.5 0 1" |
---|
52 | " ac7 25 1.1 4 0 1 rx90 tz19]"); |
---|
53 | |
---|
54 | m_option_mesh.Compile("sc#229[afcb5 1 3 0.6]" |
---|
55 | "sc#fff[afcb1 5 3 0.6 tz-1]"); |
---|
56 | } |
---|
57 | |
---|
58 | m_laser_mesh.Compile("[sc#fff scb#000 aq8 1 sx0.25 tx-3 sc#f00" |
---|
59 | " aq8 1 tx4 sz50 sx0.3 tz-200 mx as10 12 8 1 1]"); |
---|
60 | |
---|
61 | m_position = vec3(0.f + type * 80.f, 3.5f + 50.f, 0.f); |
---|
62 | |
---|
63 | m_options.Push(m_position, vec3(0.f, 0.f, 0.f)); |
---|
64 | m_options.Push(m_position, vec3(0.f, 0.f, 0.f)); |
---|
65 | m_options.Push(m_position, vec3(0.f, 0.f, 0.f)); |
---|
66 | m_options.Push(m_position, vec3(0.f, 0.f, 0.f)); |
---|
67 | } |
---|
68 | |
---|
69 | ~Player() |
---|
70 | { |
---|
71 | if (m_stick) |
---|
72 | Input::UntrackStick(m_stick); |
---|
73 | } |
---|
74 | |
---|
75 | char const *GetName() { return "<ship>"; } |
---|
76 | |
---|
77 | protected: |
---|
78 | virtual void TickGame(float seconds) |
---|
79 | { |
---|
80 | WorldEntity::TickGame(seconds); |
---|
81 | |
---|
82 | float updown = (float)(Input::GetButtonState(273 /*SDLK_UP*/) - Input::GetButtonState(274 /*SDLK_DOWN*/)); |
---|
83 | float rightleft = (float)(Input::GetButtonState(275 /*SDLK_RIGHT*/) - Input::GetButtonState(276 /*SDLK_LEFT*/)); |
---|
84 | |
---|
85 | if (!m_stick) |
---|
86 | m_stick = Input::TrackStick(); |
---|
87 | if (m_stick && m_stick->GetAxisCount() >= 4) |
---|
88 | { |
---|
89 | rightleft += 1.f * m_stick->GetAxis(m_type * 2 + 0); |
---|
90 | updown += -1.f * m_stick->GetAxis(m_type * 2 + 1); |
---|
91 | } |
---|
92 | |
---|
93 | /* Clamp direction at 45-degree multiples */ |
---|
94 | if (rightleft * rightleft + updown * updown > 0.2f) |
---|
95 | { |
---|
96 | float angle = lol::atan2(updown, rightleft); |
---|
97 | angle = (float)((int)(angle / ((float)M_PI / 4.f) + 8.5f) * ((float)M_PI / 4.f)); |
---|
98 | rightleft = lol::cos(angle); |
---|
99 | updown = lol::sin(angle); |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | rightleft = updown = 0.f; |
---|
104 | } |
---|
105 | |
---|
106 | m_damped_stick.x = m_damped_stick.x + (seconds / (seconds + 0.18f)) * (rightleft - m_damped_stick.x); |
---|
107 | m_damped_stick.z = m_damped_stick.z + (seconds / (seconds + 0.18f)) * (updown - m_damped_stick.z); |
---|
108 | vec3 applied_stick = vec3(std::abs(m_damped_stick.x), 0.0f, m_damped_stick.z); |
---|
109 | vec3 offset = vec3(((m_damped_stick.x < 0.0f)?(std::abs(m_damped_stick.x) * 30.f):(0.f)), 0.0f, 0.0f); |
---|
110 | |
---|
111 | for (int i = 0; i < m_options.Count(); i++) |
---|
112 | { |
---|
113 | vec3 rot[4] = { |
---|
114 | vec3(20.f, 0.f, 0.f), |
---|
115 | vec3(-20.f, 0.f, 0.f), |
---|
116 | vec3(20.f, 0.f, 0.f), |
---|
117 | vec3(-20.f, 0.f, 0.f) }; |
---|
118 | |
---|
119 | vec3 pos[4] = { |
---|
120 | vec3(11.f, 0.f, 0.f), |
---|
121 | vec3(-11.f, 0.f, 0.f), |
---|
122 | vec3(21.f, 0.f, 0.f), |
---|
123 | vec3(-21.f, 0.f, 0.f) }; |
---|
124 | |
---|
125 | vec3 move_pos[4] = { |
---|
126 | vec3( -8.f, 0.f, -5.f), |
---|
127 | vec3(-19.f, 0.f, -5.f), |
---|
128 | vec3( 3.f, 0.f, 5.f), |
---|
129 | vec3(-30.f, 0.f, 5.f) }; |
---|
130 | |
---|
131 | /* FIXME: this is not delta-timed */ |
---|
132 | vec3 target_pos = m_position + pos[i] + (move_pos[i] - pos[i]) * applied_stick + offset; |
---|
133 | m_options[i].m1 = m_options[i].m1 + (seconds / (seconds + 0.1f)) * (target_pos - m_options[i].m1); |
---|
134 | m_options[i].m2[0] = rot[i][0] + (((m_damped_stick.x < 0.0f)?(20.0f):(-20.0f)) - rot[i][0]) * std::abs(m_damped_stick.x); |
---|
135 | |
---|
136 | if (m_damped_stick.x < 0.3f && m_damped_stick.x > -0.3f) |
---|
137 | m_options[i].m2[2] += seconds * (i & 1 ? 600.f : -600.f); |
---|
138 | else |
---|
139 | m_options[i].m2[2] += seconds * ((m_damped_stick.x < 0.0f)?(600.0f):(-600.0f)); |
---|
140 | } |
---|
141 | |
---|
142 | if (m_damped_stick.x > 0.5f || m_damped_stick.x < -0.5f) |
---|
143 | { |
---|
144 | if (move_rotation_timer > 0.8f) |
---|
145 | m_move_rotation.z += m_damped_stick.x * -420.f * seconds; |
---|
146 | else |
---|
147 | move_rotation_timer += seconds; |
---|
148 | } |
---|
149 | else |
---|
150 | { |
---|
151 | move_rotation_timer = 0.0f; |
---|
152 | m_move_rotation.z -= m_move_rotation.z * (seconds / (seconds + 0.2f)); |
---|
153 | } |
---|
154 | |
---|
155 | m_exhaust_rotation.z += 600.0f * seconds; |
---|
156 | |
---|
157 | if (m_move_rotation.z > 180.f) |
---|
158 | m_move_rotation.z -= 360.f; |
---|
159 | else if (m_move_rotation.z < -180.f) |
---|
160 | m_move_rotation.z += 360.f; |
---|
161 | |
---|
162 | if (m_exhaust_rotation.z > 180.f) |
---|
163 | m_exhaust_rotation.z -= 360.f; |
---|
164 | else if (m_exhaust_rotation.z < -180.f) |
---|
165 | m_exhaust_rotation.z += 360.f; |
---|
166 | |
---|
167 | m_rotation = quat::fromeuler_xyz(vec3(((m_damped_stick.z <= 0.0f)?(m_damped_stick.z):(0.0f)) * -60.0f, 0.f, m_damped_stick.x * -50.0f) + m_move_rotation); |
---|
168 | |
---|
169 | if (m_type == 0) |
---|
170 | m_position += vec3(rightleft, 0.f, -updown) * 340.f * seconds; |
---|
171 | else |
---|
172 | m_position += vec3(rightleft, 0.f, -updown) * 240.f * seconds; |
---|
173 | } |
---|
174 | |
---|
175 | virtual void TickDraw(float seconds) |
---|
176 | { |
---|
177 | WorldEntity::TickDraw(seconds); |
---|
178 | |
---|
179 | if (!m_ready) |
---|
180 | { |
---|
181 | m_ship_mesh.MeshConvert(); |
---|
182 | m_option_mesh.MeshConvert(); |
---|
183 | m_laser_mesh.MeshConvert(); |
---|
184 | m_exhaust_mesh.MeshConvert(); |
---|
185 | m_ready = true; |
---|
186 | } |
---|
187 | |
---|
188 | mat4 model = mat4::translate(m_position) * mat4(m_rotation); |
---|
189 | |
---|
190 | m_ship_mesh.Render(model); |
---|
191 | |
---|
192 | if (m_type == 0) |
---|
193 | { |
---|
194 | m_exhaust_mesh.Render(model * mat4::translate(vec3(-4.f,0.f,0.f)) * mat4::fromeuler_xyz(m_exhaust_rotation * -1.0f)); |
---|
195 | m_exhaust_mesh.Render(model * mat4::translate(vec3( 4.f,0.f,0.f)) * mat4::fromeuler_xyz(m_exhaust_rotation)); |
---|
196 | } |
---|
197 | else |
---|
198 | m_exhaust_mesh.Render(model * mat4::fromeuler_xyz(m_exhaust_rotation)); |
---|
199 | |
---|
200 | for (int i = 0; i < m_options.Count(); i++) |
---|
201 | { |
---|
202 | mat4 t = mat4::translate(m_options[i].m1); |
---|
203 | |
---|
204 | bool fire = m_stick && m_stick->GetButtonCount() >= 12 |
---|
205 | && m_stick->GetButton(12); |
---|
206 | fire = fire || Input::GetButtonState(305 /* Right Ctrl */); |
---|
207 | if (fire) |
---|
208 | m_laser_mesh.Render(t); |
---|
209 | |
---|
210 | t *= mat4::fromeuler_yxz(m_options[i].m2); |
---|
211 | m_option_mesh.Render(t); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | private: |
---|
216 | int m_type; |
---|
217 | EasyMesh m_ship_mesh, m_option_mesh, m_laser_mesh, m_exhaust_mesh; |
---|
218 | Stick *m_stick; |
---|
219 | |
---|
220 | Array<vec3, vec3> m_options; |
---|
221 | |
---|
222 | vec3 m_damped_stick; |
---|
223 | vec3 m_move_rotation; |
---|
224 | vec3 m_exhaust_rotation; |
---|
225 | float move_rotation_timer; |
---|
226 | |
---|
227 | bool m_ready; |
---|
228 | }; |
---|
229 | |
---|
230 | #endif /* __PLAYER_H__ */ |
---|
231 | |
---|