A_RTS/main.lua

225 lines
5.5 KiB
Lua
Raw Normal View History

2021-03-18 16:28:10 +00:00
g3d = require "g3d"
function love.load()
-- GRAPHICS
-- GOOD PIXEL
love.graphics.setDefaultFilter("nearest")
-- FONTS
2021-03-19 18:58:05 +00:00
DefaultFont = love.graphics.newImageFont("assets/default_font.png",
2021-03-18 16:28:10 +00:00
" abcdefghijklmnopqrstuvwxyz" ..
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0" ..
"123456789.,!?-+/():;%&`'*#=[]\"")
love.graphics.setFont(DefaultFont)
--fps_count
fps_count = 0
fps_second = 0
fps_draw = 0
fps_total = 0
-- camera
g3d.camera.pivot = { 0, 0, 0 }
g3d.camera.position = { 0, 0, 0 }
g3d.camera.pitch = 0
g3d.camera.zoom = 10
2021-03-18 16:28:10 +00:00
speed = 0
2021-03-19 18:58:05 +00:00
-- player
player = {
id = 1
}
-- enums
2021-03-19 18:58:05 +00:00
require "scripts/enums"
--objects
2021-03-19 18:58:05 +00:00
require "scripts/objects"
require "scripts/units"
-- levels
2021-03-19 18:58:05 +00:00
require "scripts/levels"
2021-03-18 16:28:10 +00:00
current_level = levels.main_menu
end
function love.update(dt)
2021-03-18 16:28:10 +00:00
-- count frames per seconds
if fps_second >= 1 then
fps_second = fps_second - 1
fps_draw = fps_count
fps_count = 0
fps_total = fps_total + 1
2021-03-18 16:28:10 +00:00
end
fps_second = fps_second + dt
fps_dt = dt
fps_count = fps_count + 1
-- camera: slow, fast, medium?
2021-03-19 18:58:05 +00:00
speed = g3d.camera.zoom/2
2021-03-18 16:28:10 +00:00
2021-03-19 18:58:05 +00:00
-- do camera
2021-03-18 16:28:10 +00:00
if not game_pause then
g3d.camera.strategyViewMovement(dt,speed,is_scrolling)
-- movement: w,a,s,d,
-- rotate: q,e
-- move by screen: 20 px: sides, forward, backwards
-- mouse scroll: zoom
2021-03-18 16:28:10 +00:00
end
-- select troops
if love.mouse.isDown(1) == true then
if selecting_troops == false then
sel_pos_x = love.mouse.getX()
sel_pos_y = love.mouse.getY()
end
selecting_troops = true
else
selecting_troops = false
end
end
2021-03-19 18:58:05 +00:00
function love.wheelmoved(_, y)
-- get scroll to do zoom, and call camera update
g3d.camera.zoom = math.min(math.max(3,g3d.camera.zoom-y/1.5),30)
is_scrolling = true
2021-03-18 16:28:10 +00:00
end
function love.keypressed(key)
2021-03-19 18:58:05 +00:00
-- pause the game
2021-03-18 16:28:10 +00:00
if key == "escape" then
if game_pause then
game_pause = false
else
game_pause = true
end
end
2021-03-19 18:58:05 +00:00
-- select all units button
if key == "f1" then
for _, unit in pairs(current_level.units) do
if unit.faction == player.id then
unit.is_selected = true
end
end
end
2021-03-18 16:28:10 +00:00
end
function love.draw()
-- get drawing globals
game_width = love.graphics.getWidth()
game_height = love.graphics.getHeight()
2021-03-19 18:58:05 +00:00
-- draw every model in entities
local ent_count = 0
for _, entity in pairs(current_level.entities) do
2021-03-19 18:58:05 +00:00
draw_entity(entity)
2021-03-18 16:28:10 +00:00
end
2021-03-19 18:58:05 +00:00
-- draw every unit's troops
for _, unit in pairs(current_level.units) do for _, entity in pairs(unit.troops) do
draw_entity(entity)
end end
-- draw selection
if selecting_troops == true then
love.graphics.rectangle("line",sel_pos_x,sel_pos_y, love.mouse.getX() - sel_pos_x, love.mouse.getY() - sel_pos_y)
else
end
2021-03-18 16:28:10 +00:00
-- print coords
if game_pause then
draw_pause_menu(30,30)
else
2021-03-19 18:58:05 +00:00
draw_debug()
draw_combat_ui()
2021-03-18 16:28:10 +00:00
end
for _, entity in pairs(current_level.entities) do
if entity.model ~= nil then
if entity.is_animated then
love.graphics.print("["..entity.name.."] frame: "..entity.anim_frame.."/"..entity.anim_frames..", rm: \""..entity.rotate_mode.."\"",20,20)
love.graphics.print("["..entity.name.."] animation: "..entity.anim_path,20,40)
2021-03-18 16:28:10 +00:00
end
end
end
end
2021-03-19 18:58:05 +00:00
function draw_debug()
love.graphics.print("time: "..fps_total..", fps: "..fps_draw..", frametime: "..fps_dt..", x: "..math.floor(g3d.camera.position[1])..", y: "..math.floor(g3d.camera.position[2])..", z: "..math.floor(g3d.camera.position[3]), x, y)
end
2021-03-18 16:28:10 +00:00
function draw_pause_menu(x,y)
love.graphics.setColor(1,1,1,0.3)
love.graphics.rectangle("fill", x, y, game_width-2*x, game_height-2*y)
love.graphics.setColor(1,1,1,1)
end
2021-03-19 18:58:05 +00:00
function draw_entity(entity)
if entity.model ~= nil then
-- do animated models if animated, if anim_path and if game playing
if entity.is_animated == true and entity.anim_path ~= nil and game_pause ~= true then
-- try to animate
entity.anim_subframe = entity.anim_subframe + 1
if entity.anim_subframe >= entity.anim_speed then
entity.anim_frame = entity.anim_frame + 1
entity.anim_subframe = entity.anim_subframe - entity.anim_speed
end
-- cycle
if entity.anim_frame >= entity.anim_frames+1 then entity.anim_frame = entity.anim_frame - entity.anim_frames end
-- change
entity.model.mesh:setTexture(entity.anim_imgs[entity.anim_frame])
end
-- do rotating models if animated, if has to rotate and if game playing
if entity.rotate_mode ~= nil and entity.rotate_mode ~= "none" and game_pause ~= true then
local rm = entity.rotate_mode
if rm == "cam_xz" then
local sin = g3d.camera.position[1]-entity.model.translation[1]
local cos = g3d.camera.position[3]-entity.model.translation[3]
local angle = math.atan2(sin,cos)-math.rad(180)
entity.model:setRotation(0,angle,0)
end
end
entity.model:draw()
else -- simple loaded entities dont require so much fuss
entity:draw()
end
end
function draw_combat_ui()
local unit_count = 0
-- count how much units
for _, unit in pairs(current_level.units) do
if unit.faction == player.id then
unit_count = unit_count + 1
end
end
local unit_total = unit_count
-- position units accordingly
unit_count = 0
for _, unit in pairs(current_level.units) do
unit_count = unit_count + 1
if unit.faction == player.id then
draw_portrait(unit,unit_count,unit_total)
end
end
end
function draw_portrait(u,unit_count,unit_total)
--$-- temporal
--love.graphics.line(game_width/2,0,game_width/2,game_height)
--$--
local pos_x = game_width/2-(74*unit_total)+74*unit_count+74/2
local pos_y = game_height-83
love.graphics.draw(img.hud.unit_show, pos_x-2, pos_y, 0, 1)
love.graphics.draw(u.portrait, pos_x, pos_y+14, 0, 4)
end