-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
207 lines (166 loc) · 5.49 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
local Game = require('pkg.Game')
local Box = require('pkg.Box')
local collision = require('pkg.Collision')
local helper = require('pkg.Helper')
local SelectGroup = require('pkg.game.SelectGroup')
local gamehelper = require("pkg.game.RtsHelper")
local conf = require("pkg.game.gameconf")
local DominationPoint = require("pkg.game.DominationPoint")
local game
local selectArea
UNITS_COUNT = 18
MOOVING_SPEED = 4
UNIT_DIMENSIONS = 20
UNITS = {}
TEAM_BLUE = {}
TEAM_RED = {}
SELECTED_UNITS = {}
UNOVERLAP_SPEED = 0.2
DOM_POINTS = {}
TEAM_BLUE_CACHE = 0
SHADER = nil
CANVAS_LIGHT = nil
-- love.graphics.clear = function() end
function love.load()
if arg[#arg] == "-debug" then require("mobdebug").start() end
love.keyboard.setKeyRepeat( true )
-- Simple light shader
local shader_code = [[
struct Light {
vec2 position;
vec3 diffuse;
float power;
};
extern Light lights[332];
extern int num_lights;
extern vec2 screen;
const float constant = 1.0;
const float linear = 0.09;
const float quadratic = 0.032;
vec4 effect(vec4 color, Image image, vec2 uv, vec2 screen_coords) {
vec4 pixel = Texel(image, uv);
vec2 norm_screen = screen_coords / screen;
vec3 diffuse = vec3(0);
for (int i = 0 ; i < num_lights; i++ ) {
Light light = lights[i];
vec2 norm_pos = light.position / screen;
float distance = length(norm_pos - norm_screen) * light.power;
float attenuation = 1.0 / ( constant + linear * distance + quadratic * (distance * distance) );
diffuse += light.diffuse * attenuation;
}
diffuse = clamp(diffuse, 0.0, 1.0);
return pixel * vec4(diffuse, 1.0);
}
]]
CANVAS_LIGHT = love.graphics.newCanvas(conf._WINDOW_WIDTH, conf._WINDOW_HEIGHT)
SHADER = love.graphics.newShader(shader_code)
love.window.setMode( conf._WINDOW_WIDTH * conf._WINDOW_SCALLING_X, conf._WINDOW_HEIGHT * conf._WINDOW_SCALLING_Y, {fullscreen=conf._WINDOW_FULLSCREEN})
game = Game:new()
gamehelper.StarField(game, 1000, 1200, 1200)
selectArea = SelectGroup:new()
selectArea:setBox(Box:new())
game:observe(selectArea)
game:observeMany(UNITS)
-- Domination Points
table.insert(DOM_POINTS, DominationPoint:new{ x=100, y=1000 })
table.insert(DOM_POINTS, DominationPoint:new{ x=1400, y=100 })
table.insert(DOM_POINTS, DominationPoint:new{ x=100, y=100, tag="red"})
table.insert(DOM_POINTS, DominationPoint:new{ x=1400, y=1000, tag="blue" })
table.insert(DOM_POINTS, DominationPoint:new{ x=800, y=600, tag='blue' })
game:observeMany(DOM_POINTS)
end
function love.update(dt)
TEAM_RED = helper.ArrFilter(TEAM_RED, gamehelper.RemoveDead)
TEAM_BLUE = helper.ArrFilter(TEAM_BLUE, gamehelper.RemoveDead)
UNITS = helper.ArrFilter(UNITS, gamehelper.RemoveDead)
SELECTED_UNITS = helper.ArrFilter(SELECTED_UNITS, gamehelper.RemoveDead)
helper.ArrMap(UNITS, function(u)
u.atackTarget = nil
return u
end)
helper.ArrMap(DOM_POINTS, function(dp)
dp.red_invasors = {}
dp.blue_invasors = {}
return dp
end)
--Unoverlap
collision.SelfColide(UNITS, function(u1, u2)
gamehelper.ColideUnoverlapX(u1, u2)
gamehelper.ColideUnoverlapY(u1, u2)
end)
--Unoverlap
collision.SelfColide(UNITS, function(u1, u2)
gamehelper.ColideUnoverlapX(u1, u2)
gamehelper.ColideUnoverlapY(u1, u2)
end)
--Atack
collision.ColideWithB(TEAM_BLUE, TEAM_RED, function(u1, u2)
u1.atackTarget = u2
u2.atackTarget = u1
end, function(u1, u2)
return collision.Euclidian(u1, u2, 100)
end)
-- DominationPoint Coliision
collision.ColideWithB(UNITS, DOM_POINTS, function(u, dp)
if u.tag == "red" then
dp.red_invasors[u.id] = true
else
dp.blue_invasors[u.id] = true
end
end, function(u, dp)
return collision.Euclidian(u, dp, 50)
end)
game:update(dt)
game:animate(dt)
game:runTimer(dt)
end
function love.draw(dt)
-- love.graphics.setCanvas(CANVAS_LIGHT)
-- love.graphics.setShader(SHADER)
-- love.graphics.setColor(0,0,0, 0.005)
-- love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
-- SHADER:send("num_lights", #UNITS)
-- SHADER:send("screen", {
-- love.graphics.getWidth(),
-- love.graphics.getHeight()
-- })
-- for i=1, #UNITS do
-- SHADER:send("lights["..i.."].diffuse", {0, 0, 0})
-- end
-- for i,u in pairs(SELECTED_UNITS) do
-- SHADER:send("lights["..i.."].position", {u.x-game.camera.offsetX, u.y-game.camera.offsetY})
-- SHADER:send("lights["..i.."].diffuse", {0, 1.0, 1.0})
-- SHADER:send("lights["..i.."].power", 850)
-- end
-- love.graphics.scale(conf._WINDOW_SCALLING_X, conf._WINDOW_SCALLING_Y)
-- game:draw(dt)
-- love.graphics.setCanvas()
-- love.graphics.setShader()
-- love.graphics.draw(CANVAS_LIGHT, 0, 0)
game:draw(dt)
end
function love.mousepressed(x, y, button, istouch)
if button == 1 then
if not selectArea:isAlive() then
local offsetx = game.camera.offsetX
local offsety = game.camera.offsetY
selectArea.x = x + offsetx
selectArea.y = y + offsety
selectArea.alive = true
end
end
end
function love.mousereleased(x, y, button, istouch)
if button == 2 then
gamehelper.Agroup(x+game.camera.offsetX, y+game.camera.offsetY, SELECTED_UNITS)
elseif button == 1 then
SELECTED_UNITS = {}
-- gamehelper.SelectUnits(TEAM_BLUE, selectArea)
print(game)
selectArea:invertSquareCoordinates(game)
gamehelper.SelectUnits(UNITS, selectArea)
end
end
function love.keypressed(key, scancode, isrepeat)
gamehelper.movecam(key, game.camera)
end