-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjump_statemachine.lua
More file actions
83 lines (76 loc) · 2.88 KB
/
jump_statemachine.lua
File metadata and controls
83 lines (76 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
jump_states = {
jumping = 0,
decelerate = 1,
falling = 2,
rising = 3,
grounded = 4
}
local max_jump_frames = 15
local curr_jump_frames = 0
local max_decel_frames = 10
local curr_decel_frames = 0
function is_player_grounded()
local map_flag_1 = fget(mget(player.x, player.y+1))
local map_flag_2 = fget(mget(player.x+1, player.y+1))
local player_flag = fget(player.sprite)
return map_flag_1 == player_flag or map_flag_2 == player_flag
end
function is_player_inside_wall()
local map_flag_1 = fget(mget(player.x, player.y+0.875))
local map_flag_2 = fget(mget(player.x+0.875, player.y+0.875))
local map_flag_3 = fget(mget(player.x, player.y))
local map_flag_4 = fget(mget(player.x+0.875, player.y))
local player_flag = fget(player.sprite)
return map_flag_1 == player_flag or map_flag_2 == player_flag or map_flag_3 == player_flag or map_flag_4 == player_flag
end
function advance_state_machine()
if is_player_inside_wall() then
player.jump_state = jump_states.rising
end
if (player.jump_state == jump_states.grounded) then
if (btnp(button.x)) then
player.jump_state = jump_states.jumping
curr_jump_frames = max_jump_frames
elseif not is_player_grounded() then
player.jump_state = jump_states.falling
end
elseif (player.jump_state == jump_states.jumping) then
if (curr_jump_frames > 0) and btn(button.x) then
curr_jump_frames -= 1
else
player.jump_state = jump_states.decelerate
curr_decel_frames = max_decel_frames
end
elseif (player.jump_state == jump_states.decelerate) then
if is_player_grounded() then
player.jump_state = jump_states.grounded
elseif (curr_decel_frames > 0) then
curr_decel_frames -= 1
else
player.jump_state = jump_states.falling
end
elseif (player.jump_state == jump_states.falling) then
if is_player_grounded() then
player.jump_state = jump_states.grounded
end
elseif (player.jump_state == jump_states.rising) then
if not is_player_inside_wall() then
if is_player_grounded() then
player.jump_state = jump_states.grounded
else
player.jump_state = jump_states.falling
end
end
end
end
function get_new_y()
if (player.jump_state == jump_states.falling) then
return player.y + player.speed
elseif (player.jump_state == jump_states.jumping or player.jump_state == jump_states.rising) then
return player.y - player.speed
elseif (player.jump_state == jump_states.decelerate) then
return player.y + (player.speed * ((max_decel_frames - curr_decel_frames) / max_decel_frames))
else
return player.y
end
end