source: trunk/orbital/player.h @ 1433

Last change on this file since 1433 was 1433, checked in by sam, 9 years ago

orbital: use hexadecimal colour commands everywhere.

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