-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold.lua
More file actions
129 lines (109 loc) · 2.66 KB
/
Copy pathold.lua
File metadata and controls
129 lines (109 loc) · 2.66 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
require "core"
require "menu"
local Unit = class("Unit", ge.Node)
function Unit:init(x, y, player, sprite, type)
self.position = ge.Vector2(x, y)
self.origin = self.position
self.sprite = sprite
self.player = player
self.type = type
self.inHand = true
self.health = 7
self.attack = 4
end
local Game = ge.State("game")
function Game:Load()
print("Load game")
self.unit1 = ge.Texture("res/unit1.png")
self.unit2 = ge.Texture("res/unit2.png")
self.ship1 = ge.Texture("res/mothership1.png")
self.ship2 = ge.Texture("res/mothership2.png")
self.units = ge.List()
self.grid = ge.MpGrid(212, 160, 10, 7)
self.turn = 0
self.unit = nil
-- units
for i = 0, 3 do
self.units:add(Unit(438 + (i * 50), 100, 0, self.unit1, 0))
end
for i = 0, 3 do
self.units:add(Unit(438 + (i * 50), 500, 1, self.unit2, 0))
end
self.units:add(Unit(80, 300, 0, self.ship1, 1))
self.units:add(Unit( 950, 300, 1, self.ship2, 1))
end
function Game:Drop(unit)
local result = self.grid:add(unit)
if not result then
unit.position = unit.origin
else
unit.inHand = false
end
end
-- if clicked on enemy attack else move to target
function Game:Move(unit, target)
local enemy = self.units:getNode(target)
if enemy and enemy ~= unit then
enemy.health = math.max(enemy.health - unit.attack, 0)
if enemy.health == 0 then
if enemy.type == 1 then
gui.play("restart")
end
self.units:remove(enemy)
end
else
unit.position = target
self.grid:add(unit)
end
end
function Game:EndTurn()
self.turn = 1 - self.turn
end
function Game:Compare(unit)
if unit.player == self.turn and unit.type == 0 then
self.unit = unit
end
end
function Game:Update(dt)
-- select unit
if ge.pressed(1) then
self.units:select(self, Game.Compare)
end
-- drag unit
if self.unit and self.unit.inHand == true then
self.unit.position = ge.mouse()
end
-- drop on grid or return to hand
if ge.released(1) and self.unit and self.unit.inHand == true then
self:Drop(self.unit)
self.unit = nil
end
-- move/attack unit
if ge.pressed(1) and self.unit and self.unit.inHand == false then
local mouse = ge.mouse()
self:Move(self.unit, mouse)
end
end
function Game:Ui(dt)
gui.begin("game", 880, 20, 200, 10)
gui.label("Player turn " .. self.turn)
gui.begin("game", 850, 500, 150, 50)
if gui.button("End turn") then
self:EndTurn()
end
if ge.pressed("escape") then
ge.play("menu")
end
if gui.begin("restart", 312, 250, 400, 100)then
if gui.button("Game over") then ge.restart() end
end
end
function Game:Draw()
self.units:draw()
self.grid:draw(60, 40)
if self.unit then
local w, h = self.unit.sprite:getDimensions()
ge.push(self.unit)
ge.rectangle("line", -w/2,-h/2, w, h)
end
end