source: trunk/orbital/orbital.cpp @ 1357

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

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

File size: 5.7 KB
Line 
1//
2// Orbital
3//
4// Copyright: (c) 2012 Various People
5//
6
7#if defined HAVE_CONFIG_H
8#   include "config.h"
9#endif
10
11#if defined _WIN32
12#   include <direct.h>
13#endif
14
15#if defined _XBOX
16#   define _USE_MATH_DEFINES /* for M_PI */
17#   include <xtl.h>
18#   undef near /* Fuck Microsoft */
19#   undef far /* Fuck Microsoft again */
20#elif defined _WIN32
21#   define _USE_MATH_DEFINES /* for M_PI */
22#   define WIN32_LEAN_AND_MEAN
23#   include <windows.h>
24#   undef near /* Fuck Microsoft */
25#   undef far /* Fuck Microsoft again */
26#else
27#   include <cmath>
28#endif
29
30#if USE_SDL && defined __APPLE__
31#   include <SDL_main.h>
32#endif
33
34#include "core.h"
35#include "loldebug.h"
36
37using namespace std;
38using namespace lol;
39
40#include "orbital.h"
41
42Orbital::Orbital()
43{
44    for (int j = 0; j < 20; j++)
45    {
46        for (int i = 0; i < 20; i++)
47        {
48            m.SendCommand(((i + j) % 2) ? "sc.0,.1,.2,1,scb.0,.1,.2,1"
49                                        : "sc.0,.0,.1,1,scb.0,.0,.1,1");
50            m.SendCommand("ac4,2,44,40,0,0,ty-1,ad4,40,0,ry45");
51            m.Scale(vec3(std::sqrt(0.5f)));
52            m.Translate(vec3(i * 44 - 440, 0, j * 44 - 440));
53            m.Flush();
54        }
55    }
56
57    /* Yellow sphere */
58    m.SendCommand("sc1,1,0,1,asph10,30,20,24");
59    m.SendCommand("t0,0,60,fl");
60
61    /* Pink box */
62    m.SendCommand("sc1,0,1,1,afcb10,10,10,1,rx45,rz45");
63    m.SendCommand("t-20,20,0,fl");
64
65    /* Large meteor */
66    m.SendCommand("sc0,0,0.3,1,afcb30,30,30,5,ry45,rx45,afcb30,30,30,5");
67    m.SendCommand("t40,40,0,fl");
68
69    /* Grey/red bonus */
70    m.SendCommand("sc0.6,0.7,0.8,1,afcb7,4,7,0.6,sc1,0,0.3,1,afcb4,7,4,0.6");
71    m.SendCommand("t-40,20,-30,fl");
72
73    /* Orange/white alien */
74    m.SendCommand("sc1,0.7,0,1,afcb12,3,10,0.4,tz3,sc1,1,1,1,afcb2,10,10,0.4");
75    m.SendCommand("t0,40,-20,fl");
76    //m.SendCommand("rx20,ry30,t0,40,-20,fl");
77
78    /* Orange fire */
79    m.SendCommand("sc1,1,0,1,scb1,0,0,0,at4,1,s1.5,1,4,tz-13,ad6,5.8,1");
80    m.SendCommand("t-40,40,0,fl");
81
82    /* Lasers */
83    m.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,ty60,fl");
84
85    //m_particlesystem = new ParticleSystem();
86    //Ticker::Ref(m_particlesystem);
87
88    /* Create a camera that matches the settings of XNA Orbital */
89    m_camera = new Camera(vec3(0.f, 600.f, 50.f),
90                          vec3(0.f, 0.f, 50.f),
91                          vec3(0, 0, -1));
92    m_camera->SetRotation(quat::fromeuler_yxz(0.f, -30.f, 0.f));
93    m_camera->SetOrtho(1280.f / 3, 960.f / 3, -1000.f, 1000.f);
94    Ticker::Ref(m_camera);
95
96    /* Add tanks */
97    for (int j = 0; j < 5; j++)
98    for (int i = 0; i < 5; i++)
99    {
100        m_tanks << new Tank();
101        m_tanks.Last()->m_position = vec3(i * 80.f - 250.f, 0, j * 80.f - 100.f);
102        m_tanks.Last()->SetTarget(vec3(i * 160.f - 200.f, 0, j * 160.f - 130.f));
103        Ticker::Ref(m_tanks.Last());
104    }
105
106    /* Add player */
107    for (int i = 0; i < 2; i++)
108    {
109        m_players << new Player(i);
110        Ticker::Ref(m_players.Last());
111    }
112
113    m_auto_cam_timer = 0.0f;
114
115    m_angle = vec3(0.f);
116    m_angular_velocity = vec3(0.f);
117
118    m_ready = false;
119}
120
121void Orbital::TickGame(float seconds)
122{
123    WorldEntity::TickGame(seconds);
124
125    if (Input::GetButtonState(27 /*SDLK_ESCAPE*/))
126        Ticker::Shutdown();
127
128#if 0
129    if (m_auto_cam_timer > 0.0f)
130        m_auto_cam_timer -= seconds;
131
132    //Doing the query with actual values, cause I want to stay SDL-free for now.
133
134    int HMovement = Input::GetButtonState(275 /*SDLK_RIGHT*/) - Input::GetButtonState(276 /*SDLK_LEFT*/);
135    int VMovement = Input::GetButtonState(274 /*SDLK_DOWN*/) - Input::GetButtonState(273 /*SDLK_UP*/);
136    int RMovement = Input::GetButtonState(280 /*SDLK_PAGEUP*/) - Input::GetButtonState(281 /*SDLK_PAGEDOWN*/);
137
138    vec3 new_angular_velocity = vec3(0.0f);
139
140    if (VMovement != 0 || HMovement != 0 || RMovement != 0)
141    {
142        new_angular_velocity = vec3(HMovement, VMovement, RMovement) * 50.0f;
143        m_auto_cam_timer = 2.0f;
144    }
145    else if (m_auto_cam_timer <= 0.0f)
146    {
147        /* Order is yaw, pitch, roll */
148        new_angular_velocity = clamp(-m_angle, vec3(40.f, -20.f, -40.f),
149                                               vec3(40.f,  20.f,  40.f));
150    }
151
152    m_angular_velocity += (new_angular_velocity - m_angular_velocity)
153                          * (seconds / (seconds + 0.3f));
154    m_angle += m_angular_velocity * seconds;
155
156    /* TODO: implement "vec3 % float" or "fmod(vec3, float)" */
157    for (int n = 0; n < 3; n++)
158    {
159        if (m_angle[n] > 180.f)
160            m_angle[n] -= 360.f;
161        else if (m_angle[n] < -180.f)
162            m_angle[n] += 360.f;
163    }
164
165    /* Yaw around Y, Pitch around X, Roll around Z. Since it's the camera
166     * we want to move, use the inverse transform. */
167    mat4 anim = mat4(1.f / quat::fromeuler_yxz(m_angle));
168#endif
169
170    m_model = mat4(1.f);
171}
172
173void Orbital::TickDraw(float seconds)
174{
175    WorldEntity::TickDraw(seconds);
176
177    if (!m_ready)
178    {
179        m.SendCommand("irb");
180
181        /* FIXME: this object never cleans up */
182        m_ready = true;
183    }
184
185    Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
186
187    m.Render(m_model);
188}
189
190Orbital::~Orbital()
191{
192    for (int i = 0; i < m_tanks.Count(); i++)
193        Ticker::Unref(m_tanks[i]);
194    for (int i = 0; i < m_players.Count(); i++)
195        Ticker::Unref(m_players[i]);
196    //Ticker::Unref(m_particlesystem);
197    Ticker::Unref(m_camera);
198}
199
200int main(int argc, char **argv)
201{
202    Application app("Orbital", ivec2(800, 600), 60.0f);
203
204#if defined _MSC_VER && !defined _XBOX
205    _chdir("..");
206#elif defined _WIN32 && !defined _XBOX
207    _chdir("../..");
208#endif
209
210    new DebugFps(5, 5);
211    new Orbital();
212    app.ShowPointer(false);
213
214    app.Run();
215
216    return EXIT_SUCCESS;
217}
218
Note: See TracBrowser for help on using the repository browser.