source: trunk/games/demo-jnat/player.h @ 1717

Last change on this file since 1717 was 1717, checked in by Jnat, 8 years ago

added "player" controlled by sticks...

File size: 2.0 KB
Line 
1//
2// demo-jnat
3//
4
5
6#if !defined __PLAYER_H__
7#define __PLAYER_H__
8
9class Player : public WorldEntity
10{
11public:
12    Player()
13      : m_stick(0),
14        m_ready(false)
15    {
16                m_bodymesh.Compile("sc#f21 scb#1c1");
17        m_bodymesh.AppendFlatChamfBox(vec3(10, 30, 10), 1.f);
18                m_position = vec3(0.f,10.0f,0.f);
19    }
20
21    ~Player()
22    {
23        if (m_stick)
24            Input::UntrackStick(m_stick);
25    }
26
27    char const *GetName() { return "<player>"; }
28
29protected:
30    virtual void TickGame(float seconds)
31    {
32        WorldEntity::TickGame(seconds);
33
34        float updown = (float)(Input::GetButtonState(273 /*SDLK_UP*/) - Input::GetButtonState(274 /*SDLK_DOWN*/));
35        float rightleft = (float)(Input::GetButtonState(275 /*SDLK_RIGHT*/) - Input::GetButtonState(276 /*SDLK_LEFT*/));
36                if (!m_stick)
37            m_stick = Input::TrackStick(0);
38        if (!m_stick)
39            m_stick = Input::TrackStick(0);
40        if (m_stick && m_stick->GetAxisCount() >= 4)
41        {
42            rightleft += 1.f * m_stick->GetAxis(0);
43            updown += -1.f * m_stick->GetAxis(1);
44        }
45
46                //Rotate
47                /*
48        float test = lol::sin(0.5f) * 200.f + 60.f;
49        m_rotation *= quat::rotate(seconds * test, vec3(0, 1, 0));
50
51        m_velocity = 120.f * m_rotation.transform(vec3(0, 0, 1));
52        vec3 deltapos = seconds * m_velocity;
53                */
54
55                //Player control
56                if (rightleft * rightleft + updown * updown > 0.2f){
57                        m_velocity = vec3(-rightleft,0,updown);//120.f * m_rotation.transform(vec3(0, 0, 1));
58                        vec3 deltapos = 50.f* seconds * m_velocity;
59
60                        m_position += deltapos;
61                }
62
63    }
64
65    virtual void TickDraw(float seconds)
66    {
67        WorldEntity::TickDraw(seconds);
68
69        if (!m_ready)
70        {
71            m_bodymesh.MeshConvert();
72            m_ready = true;
73        }
74
75        mat4 model = mat4::translate(m_position) * mat4(m_rotation);
76
77        m_bodymesh.Render(model);
78    }
79
80private:
81    EasyMesh m_bodymesh;
82    Stick *m_stick;
83       
84    bool m_ready;
85};
86
87#endif /* __PLAYER_H__ */
88
Note: See TracBrowser for help on using the repository browser.