source: trunk/mrpigeon/player.cpp @ 1300

Last change on this file since 1300 was 1300, checked in by sam, 8 years ago

core: prefix some member variables with m_ for clarity.

  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1//
2// MrPigeon
3//
4// Copyright: (c) 2012 Various People
5//
6
7#if defined HAVE_CONFIG_H
8#   include "config.h"
9#endif
10
11#if USE_SDL && defined __APPLE__
12#   include <SDL_main.h>
13#endif
14
15#include "core.h"
16#include "debug/fps.h"
17
18using namespace std;
19using namespace lol;
20
21#include "mrpigeon.h"
22#include "player.h"
23#include "enemy.h"
24
25#define GRAVITY 0.0007f
26#define JUMPVEL 0.43f
27
28#define LEN_KICKAIR 500.0f
29#define LEN_KICKFLOOR 300.0f
30
31Player::Player(MrPigeon *in_game) :
32    game(in_game),
33    active_set(NULL),
34    active_frame(0),
35    walktime(0.0f),
36    flytime(0.0f)
37{
38    pigeon_flying = Tiler::Register("mrpigeon/gfx/pigeon_flying.png", ivec2(16,16), ivec2(0)); /* 2 frames */
39
40    player_dashdown = Tiler::Register("mrpigeon/gfx/player_DashDown.png", ivec2(16,32), ivec2(0));
41    player_death = Tiler::Register("mrpigeon/gfx/player_Death.png", ivec2(16,32), ivec2(0)); /* 7 frames */
42    player_fall = Tiler::Register("mrpigeon/gfx/player_Fall.png", ivec2(16,32), ivec2(0));
43    player_jump = Tiler::Register("mrpigeon/gfx/player_Jump.png", ivec2(16,32), ivec2(0));
44    player_kickair = Tiler::Register("mrpigeon/gfx/player_kickAir.png", ivec2(16,32), ivec2(0)); /* 3 frames */
45    player_kickfloor = Tiler::Register("mrpigeon/gfx/player_kickFloor.png", ivec2(32,32), ivec2(0)); /* 3 frames */
46    player_walkb = Tiler::Register("mrpigeon/gfx/player_WalkB.png", ivec2(16,32), ivec2(0)); /* 8 frames */
47    player_walk = Tiler::Register("mrpigeon/gfx/player_Walk.png", ivec2(16,32), ivec2(0)); /* 8 frames */
48    player_walkraisefist = Tiler::Register("mrpigeon/gfx/player_WalkRaiseFist.png", ivec2(16,32), ivec2(0)); /* 8 frames */
49
50    m_position = vec3(120.0f, 400.0f, 5.0f);
51    m_velocity = vec3(0.f);
52    state = FALLING;
53}
54
55void Player::Action()
56{
57    switch (state)
58    {
59    case WALKING:
60    case FUNKING:
61        {
62            Enemy *const *enemies = game->GetEnemies();
63            bool must_kick = false;
64            for (int i = 0; i < 10; i++)
65                if (enemies[i]
66                     && enemies[i]->m_position.x > m_position.x + 60.0f - 30.0f
67                     && enemies[i]->m_position.x < m_position.x + 60.0f + 30.0f)
68                {
69                    must_kick = true;
70                    break;
71                }
72            if (must_kick)
73            {
74                walktime = 0.0f;
75                state = KICKFLOOR;
76            }
77            else
78            {
79                m_velocity.y = JUMPVEL;
80                state = JUMPING;
81            }
82        }
83        break;
84    case JUMPING:
85        walktime = 0.0f;
86        state = KICKAIR;
87        break;
88    case FALLING:
89        m_velocity.y = -JUMPVEL;
90        state = DASHDOWN;
91        break;
92    default:
93        break;
94    }
95}
96
97void Player::TickGame(float deltams)
98{
99    WorldEntity::TickGame(deltams);
100
101    deltams *= game->GetSpeed();
102
103    flytime = fmod(deltams + flytime, 400.0f);;
104
105    vec3 oldvel = m_velocity;
106    switch (state)
107    {
108    case WALKING:
109    case FUNKING:
110        walktime = fmod(deltams + walktime, 700.0f);;
111        break;
112    case KICKFLOOR:
113        walktime += deltams;
114        if (walktime > LEN_KICKFLOOR)
115            state = WALKING;
116        break;
117    case FALLING:
118        m_velocity.y -= deltams * GRAVITY;
119        m_position += (oldvel + m_velocity) * (0.5 * deltams);
120        if (m_position.y < 81.0f)
121        {
122            m_position.y = 81.0f;
123            state = WALKING;
124        }
125        break;
126    case JUMPING:
127        m_velocity.y -= deltams * GRAVITY;
128        m_position += (oldvel + m_velocity) * (0.5 * deltams);
129        if (m_velocity.y < 0.0f)
130            state = FALLING;
131        break;
132    case KICKAIR:
133        walktime += deltams;
134        if (walktime >= LEN_KICKAIR)
135        {
136            m_velocity.y = 0.0f;
137            state = FALLING;
138        }
139        break;
140    case DASHDOWN:
141        m_velocity.y -= deltams * GRAVITY;
142        m_position += (oldvel + m_velocity) * (0.5 * deltams);
143        if (m_position.y < 81.0f)
144        {
145            m_position.y = 81.0f;
146            state = FUNKING;
147        }
148        break;
149    }
150
151    switch (state)
152    {
153    case WALKING:
154        active_set = player_walk;
155        active_frame = (int)(walktime * 8 / 700.0f);
156        break;
157    case FUNKING:
158        active_set = player_walkraisefist;
159        active_frame = (int)(walktime * 8 / 700.0f);
160        break;
161    case KICKFLOOR:
162        active_set = player_kickfloor;
163        active_frame = (int)(walktime * 3 / LEN_KICKFLOOR);
164        break;
165    case FALLING:
166        active_set = player_fall;
167        active_frame = 0;
168        break;
169    case JUMPING:
170        active_set = player_jump;
171        active_frame = 0;
172        break;
173    case KICKAIR:
174        active_set = player_kickair;
175        active_frame = (int)(walktime * 3 / LEN_KICKAIR);
176        break;
177    case DASHDOWN:
178        active_set = player_dashdown;
179        active_frame = 0;
180        break;
181    }
182}
183
184void Player::TickDraw(float deltams)
185{
186    WorldEntity::TickDraw(deltams);
187
188    Scene::GetDefault()->AddTile(pigeon_flying, (int)(flytime * 2 / 400.0f), ivec3(81, 147, 5), 0, vec2(3.0f));
189
190    Scene::GetDefault()->AddTile(active_set, active_frame, ivec3(m_position), 0, vec2(3.0f));
191}
192
193Player::~Player()
194{
195    Tiler::Deregister(pigeon_flying);
196
197    Tiler::Deregister(player_dashdown);
198    Tiler::Deregister(player_death);
199    Tiler::Deregister(player_fall);
200    Tiler::Deregister(player_jump);
201    Tiler::Deregister(player_kickair);
202    Tiler::Deregister(player_kickfloor);
203    Tiler::Deregister(player_walkb);
204    Tiler::Deregister(player_walk);
205    Tiler::Deregister(player_walkraisefist);
206}
207
Note: See TracBrowser for help on using the repository browser.