-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.lua
More file actions
143 lines (126 loc) · 4.1 KB
/
player.lua
File metadata and controls
143 lines (126 loc) · 4.1 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Player = {}
-- Constructor
function Player:new()
require "ranger"
require "shield"
require "rogue"
local object = {
characters = {},
currentChar = 1,
turnState = "",
useItemMode = false,
canUseItem = true,
useItemDelay = 0,
turnPoints = 0
}
setmetatable(object, { __index = Player })
return object
end
-- Update movement
function Player:update(board)
if self.turnPoints > 0 then
if joystick:isDown(1) and self.released then
if not board:get_tile(board.selectedTile[1], board.selectedTile[2]).isCharOn then
local type = board:get_tile(board.selectedTile[1], board.selectedTile[2]).tileType
if type == board.blankTile then
self:move(board)
elseif type == board.blockTile then
self:push(board)
end
end
self.released = false
self.turnPoints = self.turnPoints - 1
end
if joystick:isDown(5) and self.released then
self.currentChar = ((self.currentChar + 1) % #(self.characters))
if self.currentChar == 0 then
self.currentChar = #(self.characters)
end
self.released = false
self.turnPoints = self.turnPoints - 1
end
end
if self:current_char().heldItem == nil then
self:check_items(board)
end
if not joystick:isDown(1, 2, 3, 4, 5, 6, 7) then
self.released = true
end
end
-- Draw player & player items
function Player:draw()
for i, char in ipairs(self.characters) do
char:draw()
end
end
-------------------------------
-------------------------------
function Player:add_ranger()
table.insert(self.characters, Ranger:new())
end
function Player:add_shield()
table.insert(self.characters, Shield:new())
end
function Player:add_rogue()
table.insert(self.characters, Rogue:new())
end
-- Place player at game start
function Player:go_to_start_position(board)
local startTiles = choose_n(board.blankTiles, #self.characters)
for i, char in ipairs(self.characters) do
char.x = board.blankTiles[startTiles[i]].x
char.y = board.blankTiles[startTiles[i]].y
board.blankTiles[startTiles[i]].isCharOn = true
end
end
function Player:tile_in_range(tilePos)
return tile_distance(self:current_char():position(), tilePos) <= self.dice
end
function Player:current_char()
return self.characters[self.currentChar]
end
function Player:finish_turn()
self.turnState = self.turnFinished
self.currentChar = ((self.currentChar + 1) % #(self.characters))
if self.currentChar == 0 then
self.currentChar = #(self.characters)
end
end
function Player:move(board)
board:get_tile(self:current_char().x, self:current_char().y).isCharOn = false
self:current_char().x = board.selectedTile[1]
self:current_char().y = board.selectedTile[2]
board:get_tile(self:current_char().x, self:current_char().y).isCharOn = true
end
function Player:push(board)
if self:current_char().x == board.selectedTile[1] then
if self:current_char().y > board.selectedTile[2] then
board:push_to_the_max_up()
elseif self:current_char().y < board.selectedTile[2] then
board:push_to_the_max_down()
end
elseif self:current_char().y == board.selectedTile[2] then
if self:current_char().x > board.selectedTile[1] then
board:push_to_the_max_left()
elseif self:current_char().x < board.selectedTile[1] then
board:push_to_the_max_right()
end
end
end
function Player:check_items(board)
for i, item in ipairs(board.items) do
if self:current_char().x == item.x and self:current_char().y == item.y then
self:current_char().heldItem = item
table.remove(board.items, i)
end
end
end
function get_blank_tiles_y(board)
local btiles = {}
for _, t in ipairs(board.tiles) do
if t.x == 1 and t.tileType == board.blankTile then
table.insert(btiles, t.y)
end
end
return btiles
end