source: trunk/orbital/player.h @ 1357

Last change on this file since 1357 was 1357, checked in by sam, 11 years ago

orbital: clamp ship heading to full 45-degree orientations.

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1//
2// Orbital
3//
4// Copyright: (c) 2012 Various People
5//
6
7/* FIXME: this file is pure crap; it's only a test. */
8
9#if !defined __PLAYER_H__
10#define __PLAYER_H__
11
12class Player : public WorldEntity
13{
14public:
15    Player(int type)
16      : m_stick(0),
17        m_ready(false)
18    {
19        vec4 color[2] = { vec4(0.8f,0.2f,0.1f,1.0f),
20                          vec4(0.1f,0.1f,0.6f,1.0f) };
21
22        if (type == 0)
23        {
24            m_ship_mesh.SetCurColor(color[type]);
25            m_ship_mesh.SendCommand("afcb3,6,7,.4,t0,0,7,sc1,1,1,1,afcb3,4,4,.4,t4,0,-4,mx,fl");
26            m_ship_mesh.SetCurColor(color[type]);
27            m_ship_mesh.SendCommand("afcb3,6,5,.4,sc1,1,1,1,afcb2,3,9,.4,fl");
28            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");
29
30            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,t-3,0,27,mx,fl");
31        }
32        else
33        {
34            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");
35            m_ship_mesh.SetCurColor(color[type]);
36            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");
37
38            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,25");
39        }
40
41        m_option_mesh.SetCurColor(color[type]);
42        m_option_mesh.SendCommand("afcb5,1,3,0.6,fl,sc1,1,1,1,afcb1,5,3,0.6,tz-1,fl");
43
44        m_position = vec3(0.f + type * 80.f, 3.5f + 50.f, 0.f);
45
46        m_options.Push(vec3(0.f), vec3(20.f, 0.f, 0.f));
47        m_options.Push(vec3(0.f), vec3(-20.f, 0.f, 0.f));
48        m_options.Push(vec3(0.f), vec3(20.f, 0.f, 0.f));
49        m_options.Push(vec3(0.f), vec3(-20.f, 0.f, 0.f));
50    }
51
52    ~Player()
53    {
54        if (m_stick)
55            Input::UntrackStick();
56    }
57
58    char const *GetName() { return "<ship>"; }
59
60protected:
61    virtual void TickGame(float seconds)
62    {
63        WorldEntity::TickGame(seconds);
64
65        float updown = Input::GetButtonState(273 /*SDLK_UP*/)
66                     - Input::GetButtonState(274 /*SDLK_DOWN*/);
67        float rightleft = Input::GetButtonState(275 /*SDLK_RIGHT*/)
68                        - Input::GetButtonState(276 /*SDLK_LEFT*/);
69
70        if (!m_stick)
71            m_stick = Input::TrackStick();
72        if (m_stick && m_stick->GetAxisCount() >= 4)
73        {
74            rightleft += 1.f * m_stick->GetAxis(2);
75            updown += -1.f * m_stick->GetAxis(3);
76        }
77
78        /* Clamp direction at 45-degree multiples */
79        if (rightleft * rightleft + updown * updown > 0.2f)
80        {
81            float norm = sqrt(rightleft * rightleft + updown * updown);
82            float angle = atan2(updown, rightleft);
83            angle = (int)(angle / (M_PI / 4.f) + 8.5f) * (M_PI / 4.f);
84            rightleft = cos(angle);
85            updown = sin(angle);
86        }
87        else
88        {
89            rightleft = updown = 0.f;
90        }
91
92        for (int i = 0; i < m_options.Count(); i++)
93        {
94            vec3 pos[4] = { vec3(11.f, 0.f, 0.f),
95                            vec3(-11.f, 0.f, 0.f),
96                            vec3(21.f, 0.f, 0.f),
97                            vec3(-21.f, 0.f, 0.f) };
98            /* FIXME: this is not delta-timed */
99            m_options[i].m1 = 0.1f * (m_position + pos[i])
100                            + 0.9f * m_options[i].m1;
101            m_options[i].m2[2] += seconds * (i & 1 ? 600.f : -600.f);
102        }
103
104        m_position += vec3(rightleft, 0.f, -updown) * 200.f * seconds;
105    }
106
107    virtual void TickDraw(float seconds)
108    {
109        WorldEntity::TickDraw(seconds);
110
111        if (!m_ready)
112        {
113            m_ship_mesh.SendCommand("irb");
114            m_exhaust_mesh.SendCommand("irb");
115            m_option_mesh.SendCommand("irb");
116            m_ready = true;
117        }
118
119        mat4 model = mat4::translate(m_position) * mat4(m_rotation);
120        m_ship_mesh.Render(model);
121        m_exhaust_mesh.Render(model);
122
123        for (int i = 0; i < m_options.Count(); i++)
124        {
125            mat4 t = mat4::translate(m_options[i].m1)
126                   * mat4::fromeuler_yxz(m_options[i].m2);
127
128            m_option_mesh.Render(t);
129        }
130    }
131
132private:
133    Mesh m_ship_mesh, m_option_mesh, m_exhaust_mesh;
134    Stick *m_stick;
135
136    Array<vec3, vec3> m_options;
137
138    bool m_ready;
139};
140
141#endif /* __PLAYER_H__ */
142
Note: See TracBrowser for help on using the repository browser.