forked from kinbei/lua-aoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
84 lines (69 loc) · 1.86 KB
/
player.lua
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
local AVATAR_TYPE_PLAYER = 1
local AVATAR_EVENT_ADD = 1
local AVATAR_EVENT_DEL = 2
local AVATAR_EVENT_MOV = 3
local function get_avatar_id(self)
return self.avatar_id
end
local function gen_del_avatar_event(self)
return {
avatar_id = self.avatar_id,
nickname = self.nickname,
event_type = AVATAR_EVENT_DEL,
}
end
local function gen_add_avatar_event(self)
return {
avatar_id = self.avatar_id,
nickname = self.nickname,
event_type = AVATAR_EVENT_ADD,
}
end
local function gen_mov_avatar_event(self)
return {
avatar_id = self.avatar_id,
nickname = self.nickname,
event_type = AVATAR_EVENT_MOV,
}
end
local function need_aoi_process()
return true
end
local function push_aoi(self, event)
self.aoi_list[#self.aoi_list + 1] = event
end
local function get_avatar_type()
return AVATAR_TYPE_PLAYER
end
local function clear_aoi(self)
self.aoi_list = {}
end
local function gen_player_pos(map, grid_x, grid_y)
assert(map.map_grid[grid_x])
local grid = assert(map.map_grid[grid_x][grid_y])
local x = math.random(grid.left, grid.right)
local y = math.random(grid.top, grid.bottom)
return x, y
end
local function create(avatar_id, map, grid_x, grid_y)
local player = {}
--
player.avatar_id = avatar_id
player.nickname = string.format("player%d", avatar_id)
player.x, player.y = gen_player_pos(map, grid_x, grid_y)
player.grid_x = grid_x
player.grid_y = grid_y
player.aoi_list = {}
-- Functions for player
player.get_avatar_id = get_avatar_id
player.gen_del_avatar_event = gen_del_avatar_event
player.gen_add_avatar_event = gen_add_avatar_event
player.gen_mov_avatar_event = gen_mov_avatar_event
player.need_aoi_process = need_aoi_process
player.push_aoi = push_aoi
player.get_avatar_type = get_avatar_type
--
player.clear_aoi = clear_aoi
return player
end
return create