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 | |
---|
12 | class Player : public WorldEntity |
---|
13 | { |
---|
14 | public: |
---|
15 | Player(int type) |
---|
16 | : m_type(type), |
---|
17 | m_stick(0), |
---|
18 | m_ready(false) |
---|
19 | { |
---|
20 | vec4 color[2] = { vec4(0.8f,0.2f,0.1f,1.0f), |
---|
21 | vec4(0.1f,0.1f,0.6f,1.0f) }; |
---|
22 | |
---|
23 | if (type == 0) |
---|
24 | { |
---|
25 | m_ship_mesh.SetCurColor(color[type]); |
---|
26 | m_ship_mesh.SendCommand("afcb3,6,7,.4,t0,0,7,sc1,1,1,1,afcb3,4,4,.4,t4,0,-4,mx,fl"); |
---|
27 | m_ship_mesh.SetCurColor(color[type]); |
---|
28 | m_ship_mesh.SendCommand("afcb3,6,5,.4,sc1,1,1,1,afcb2,3,9,.4,fl"); |
---|
29 | 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"); |
---|
30 | |
---|
31 | 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"); |
---|
32 | } |
---|
33 | else |
---|
34 | { |
---|
35 | 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"); |
---|
36 | m_ship_mesh.SetCurColor(color[type]); |
---|
37 | 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"); |
---|
38 | |
---|
39 | 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"); |
---|
40 | } |
---|
41 | |
---|
42 | m_option_mesh.SetCurColor(color[type]); |
---|
43 | m_option_mesh.SendCommand("afcb5,1,3,0.6,fl,sc1,1,1,1,afcb1,5,3,0.6,tz-1,fl"); |
---|
44 | |
---|
45 | m_position = vec3(0.f + type * 80.f, 3.5f + 50.f, 0.f); |
---|
46 | |
---|
47 | m_options.Push(m_position, vec3(0.f, 0.f, 0.f)); |
---|
48 | m_options.Push(m_position, vec3(0.f, 0.f, 0.f)); |
---|
49 | m_options.Push(m_position, vec3(0.f, 0.f, 0.f)); |
---|
50 | m_options.Push(m_position, vec3(0.f, 0.f, 0.f)); |
---|
51 | } |
---|
52 | |
---|
53 | ~Player() |
---|
54 | { |
---|
55 | if (m_stick) |
---|
56 | Input::UntrackStick(m_stick); |
---|
57 | } |
---|
58 | |
---|
59 | char const *GetName() { return "<ship>"; } |
---|
60 | |
---|
61 | protected: |
---|
62 | virtual void TickGame(float seconds) |
---|
63 | { |
---|
64 | WorldEntity::TickGame(seconds); |
---|
65 | |
---|
66 | float updown = Input::GetButtonState(273 /*SDLK_UP*/) |
---|
67 | - Input::GetButtonState(274 /*SDLK_DOWN*/); |
---|
68 | float rightleft = Input::GetButtonState(275 /*SDLK_RIGHT*/) |
---|
69 | - Input::GetButtonState(276 /*SDLK_LEFT*/); |
---|
70 | |
---|
71 | if (!m_stick) |
---|
72 | m_stick = Input::TrackStick(); |
---|
73 | if (m_stick && m_stick->GetAxisCount() >= 4) |
---|
74 | { |
---|
75 | rightleft += 1.f * m_stick->GetAxis(m_type * 2 + 0); |
---|
76 | updown += -1.f * m_stick->GetAxis(m_type * 2 + 1); |
---|
77 | } |
---|
78 | |
---|
79 | /* Clamp direction at 45-degree multiples */ |
---|
80 | if (rightleft * rightleft + updown * updown > 0.2f) |
---|
81 | { |
---|
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 | damped_stick.x = damped_stick.x + (seconds / (seconds + 0.18f)) * (rightleft - damped_stick.x); |
---|
93 | damped_stick.z = damped_stick.z + (seconds / (seconds + 0.18f)) * (updown - damped_stick.z); |
---|
94 | vec3 applied_stick = vec3(abs(damped_stick.x), 0.0f, damped_stick.z); |
---|
95 | vec3 offset = vec3(((damped_stick.x < 0.0f)?(abs(damped_stick.x) * 30.f):(0.f)), 0.0f, 0.0f); |
---|
96 | |
---|
97 | for (int i = 0; i < m_options.Count(); i++) |
---|
98 | { |
---|
99 | vec3 rot[4] = { |
---|
100 | vec3(20.f, 0.f, 0.f), |
---|
101 | vec3(-20.f, 0.f, 0.f), |
---|
102 | vec3(20.f, 0.f, 0.f), |
---|
103 | vec3(-20.f, 0.f, 0.f) }; |
---|
104 | |
---|
105 | vec3 pos[4] = { |
---|
106 | vec3(11.f, 0.f, 0.f), |
---|
107 | vec3(-11.f, 0.f, 0.f), |
---|
108 | vec3(21.f, 0.f, 0.f), |
---|
109 | vec3(-21.f, 0.f, 0.f) }; |
---|
110 | |
---|
111 | vec3 move_pos[4] = { |
---|
112 | vec3( -8.f, 0.f, -5.f), |
---|
113 | vec3(-19.f, 0.f, -5.f), |
---|
114 | vec3( 3.f, 0.f, 5.f), |
---|
115 | vec3(-30.f, 0.f, 5.f) }; |
---|
116 | |
---|
117 | /* FIXME: this is not delta-timed */ |
---|
118 | vec3 target_pos = m_position + pos[i] + (move_pos[i] - pos[i]) * applied_stick + offset; |
---|
119 | m_options[i].m1 = m_options[i].m1 + (seconds / (seconds + 0.1f)) * (target_pos - m_options[i].m1); |
---|
120 | m_options[i].m2[0] = rot[i][0] + (((damped_stick.x < 0.0f)?(20.0f):(-20.0f)) - rot[i][0]) * abs(damped_stick.x); |
---|
121 | |
---|
122 | if (damped_stick.x < 0.00001f && damped_stick.x > -0.00001f) |
---|
123 | m_options[i].m2[2] += seconds * (i & 1 ? 600.f : -600.f); |
---|
124 | else |
---|
125 | m_options[i].m2[2] += seconds * ((damped_stick.x < 0.0f)?(600.0f):(-600.0f)); |
---|
126 | } |
---|
127 | |
---|
128 | m_position += vec3(rightleft, 0.f, -updown) * 200.f * seconds; |
---|
129 | } |
---|
130 | |
---|
131 | virtual void TickDraw(float seconds) |
---|
132 | { |
---|
133 | WorldEntity::TickDraw(seconds); |
---|
134 | |
---|
135 | if (!m_ready) |
---|
136 | { |
---|
137 | m_ship_mesh.SendCommand("irb"); |
---|
138 | m_exhaust_mesh.SendCommand("irb"); |
---|
139 | m_option_mesh.SendCommand("irb"); |
---|
140 | m_ready = true; |
---|
141 | } |
---|
142 | |
---|
143 | mat4 model = mat4::translate(m_position) * mat4(m_rotation); |
---|
144 | m_ship_mesh.Render(model); |
---|
145 | m_exhaust_mesh.Render(model); |
---|
146 | |
---|
147 | for (int i = 0; i < m_options.Count(); i++) |
---|
148 | { |
---|
149 | mat4 t = mat4::translate(m_options[i].m1) |
---|
150 | * mat4::fromeuler_yxz(m_options[i].m2); |
---|
151 | |
---|
152 | m_option_mesh.Render(t); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | private: |
---|
157 | int m_type; |
---|
158 | Mesh m_ship_mesh, m_option_mesh, m_exhaust_mesh; |
---|
159 | Stick *m_stick; |
---|
160 | |
---|
161 | Array<vec3, vec3> m_options; |
---|
162 | |
---|
163 | vec3 damped_stick; |
---|
164 | |
---|
165 | bool m_ready; |
---|
166 | }; |
---|
167 | |
---|
168 | #endif /* __PLAYER_H__ */ |
---|
169 | |
---|