source: trunk/orbital/player.h @ 1434

Last change on this file since 1434 was 1434, checked in by sam, 10 years ago

orbital: enhance the language with [] contexts.

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