/
archmage.lua
165 lines (148 loc) · 5.28 KB
/
archmage.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
-- Onoroubos
-- Copyright 2009-2010 Christiaan Janssen, December 2009 - January 2010
--
-- This file is part of Onoroubos.
--
-- Onoroubos is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- Onoroubos is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with Onoroubos. If not, see <http://www.gnu.org/licenses/>.
Archmage = {}
function Archmage.load()
Buildings.generate_map()
-- generate schools
Archmage.school_count = 7
Archmage.schools = {}
for i=1,Archmage.school_count do
Archmage.schools[i] = Archmage.newschool()
end
-- selection and map
Archmage.selection ={
active = true,
number = 1,
box = { 0,0,0,0 },
}
Archmage.menu = {
active = false,
option = Cte.wl_menu.move,
}
end
function Archmage.newschool()
local nx,ny = math.floor(screensize[1]/Game.map_cellwidth), math.floor(screensize[2]/Game.map_cellheight)
local ix,iy = math.random(nx-2)+1,math.random(ny-2)+1
while Buildings.map[ix][iy] and Buildings.map[ix][iy].is_school do
ix,iy = math.random(nx-2)+1,math.random(ny-2)+1
end
local px,py = math.floor((ix-0.5)*Game.map_cellwidth),math.floor((iy-0.5)*Game.map_cellheight)
local school= Buildings.new_building( 2, px, py )
school.is_school = true
Sprites.updateInBuffer( school )
if Buildings.map[ix] and Buildings.map[ix][iy] then
Sprites.disappear( Buildings.map[ix][iy] )
Buildings.map[ix][iy]=nil
end
Buildings.map[ix][iy] = school
school.count = 1
school.stats = {
max_hp = 100,
hp = 100,
attack = 0,
defense = 0,
speed = 0,
range = 0,
}
school.center = { school.pos[1], school.pos[2] }
school.list={school}
school.mygroup = school
return school
end
function Archmage.update(dt)
-- update school timers
Archmage.school_count = 0
for i=1,table.getn(Archmage.schools) do
if Archmage.schools[i].stats.hp <= 0 then
Archmage.schools[i].count=0
else
Archmage.school_count = Archmage.school_count + 1
end
end
end
function Archmage.draw()
-- draw selection
if Archmage.selection.active then
local ml = 5
local school = Archmage.schools[Archmage.selection.number]
Archmage.selection.box = { math.floor(school.pos[1] - 24), math.floor(school.pos[2] - 56),
math.floor(school.pos[1] + 24), math.floor(school.pos[2] + 24) }
love.graphics.setColor( Sprites.archmageColor )
love.graphics.setLine( 2, love.line_rough )
local box = Archmage.selection.box
love.graphics.line(box[1]-1,box[2],box[1]+ml-1,box[2])
love.graphics.line(box[1],box[2]-1,box[1],box[2]+ml-1)
love.graphics.line(box[1]-1,box[4],box[1]+ml-1,box[4])
love.graphics.line(box[1],box[4],box[1],box[4]-ml+1)
love.graphics.line(box[3],box[2],box[3]-ml+1,box[2])
love.graphics.line(box[3],box[2]-1,box[3],box[2]+ml-1)
love.graphics.line(box[3]+1,box[4],box[3]-ml+1,box[4])
love.graphics.line(box[3],box[4]+1,box[3],box[4]-ml+1)
end
-- draw menu
end
function Archmage.keypressed( key )
if key == love.key_up then
if Archmage.menu.active then Archmage.move_menu( Cte.dircodes.up ) else
Archmage.move_selection( Cte.dircodes.up ) end
end
if key == love.key_down then
if Archmage.menu.active then Archmage.move_menu( Cte.dircodes.down ) else
Archmage.move_selection( Cte.dircodes.down ) end
end
if key == love.key_left then
if Archmage.menu.active then Archmage.move_menu( Cte.dircodes.left ) else
Archmage.move_selection( Cte.dircodes.left ) end
end
if key == love.key_right then
if Archmage.menu.active then Archmage.move_menu( Cte.dircodes.right ) else
Archmage.move_selection( Cte.dircodes.right ) end
end
end
function Archmage.keyreleased( key )
end
function Archmage.move_menu( direction )
end
function Archmage.move_selection( direction )
-- find the nearest school that is in direction
local selection = Archmage.schools[ Archmage.selection.number ]
local original = selection
local min_distance = screensize[1]*screensize[1] + screensize[2]*screensize[2]
local index = Archmage.selection.number
for i,school in ipairs(Archmage.schools) do
if school ~= original and
--~ school.count > 0 and
((direction == Cte.dircodes.up and school.pos[2] < original.pos[2] ) or
(direction == Cte.dircodes.down and school.pos[2] > original.pos[2] ) or
(direction == Cte.dircodes.left and school.pos[1] < original.pos[1] ) or
(direction == Cte.dircodes.right and school.pos[1] > original.pos[1] )) --and
--~ (((direction == Cte.dircodes.up or direction == Cte.dircodes.down) and
--~ math.abs(school.pos[2]-original.pos[2]) >= math.abs(school.pos[1]-original.pos[1]) ) or
--~ ((direction == Cte.dircodes.left or direction == Cte.dircodes.right) and
--~ math.abs(school.pos[2]-original.pos[2]) <= math.abs(school.pos[1]-original.pos[1]) ))
then
local distance = mymath.get_distance(school.pos, original.pos)
if distance < min_distance then
min_distance = distance
selection = school
index = i
end
end
end
Archmage.selection.number = index
end