A_RTS/main.lua

269 lines
6.7 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
-- debug
scroll = 0
debug = false
2021-03-19 18:58:05 +00:00
-- player
player = {
id = 1
}
-- enums
2021-03-19 18:58:05 +00:00
require "scripts/enums"
2021-03-20 10:17:05 +00:00
-- functions
require "scripts/drawing_UI"
--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?
speed = 8
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)
local center = -500
local pos = {g3d.camera.position[1],g3d.camera.position[2]+center,g3d.camera.position[3]}
current_level.skybox:setTransform(pos)
-- 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()
has_troops_selected = false
end
selecting_troops = true
else
if selecting_troops == true then
-- somehow select entities in area
has_troops_selected = true
end
selecting_troops = false
end
if has_troops_selected then
end
end
2021-03-19 18:58:05 +00:00
function love.wheelmoved(_, y)
if debug then
scroll = scroll+15*y
else
2021-03-19 18:58:05 +00:00
-- 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
end
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 == "f2" then
if has_troops_selected then
has_troops_selected = false
else
has_troops_selected = true
2021-03-19 18:58:05 +00:00
end
for _, u in pairs(current_level.units) do for _, entity in pairs(current_level.entities) do
if entity.unit ~= nil and entity.unit == u.id then
if has_troops_selected then
entity.is_selected = false
entity.selected_model = nil
else
entity.is_selected = true
entity.selected_model = g3d.newModel("assets/objects/horizontal_plane.obj",img.effects.is_selected,{entity.pos[1],entity.pos[2]+0.5,entity.pos[3]},{0,0,0},{16,16,16})
end
end
end end
end
if key == "f1" then
if debug then debug = false else debug = true end
2021-03-19 18:58:05 +00:00
end
2021-03-18 16:28:10 +00:00
end
function love.draw()
-- get drawing globals
2021-03-18 16:28:10 +00:00
game_width = love.graphics.getWidth()
game_height = love.graphics.getHeight()
-- LAYER 1: THE WORLD
draw_entity(current_level.skybox)
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
if entity.unit == nil then draw_entity(entity) end
2021-03-18 16:28:10 +00:00
end
2021-03-19 18:58:05 +00:00
-- draw every unit's troops
for _, u in pairs(current_level.units) do for _, entity in pairs(current_level.entities) do
if entity.unit ~= nil and entity.unit == u.id then draw_entity(entity) end
end end
-- LAYER 2: THE USER INTERFACE
if game_pause then
draw_pause_menu(30,30)
else
draw_combat_ui()
end
-- LAYER 3: INPUT & DEBUG
-- draw selection area
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)
2021-03-18 16:28:10 +00:00
end
-- debug
if debug then draw_debug() end
2021-03-18 16:28:10 +00:00
end
2021-03-20 10:17:05 +00:00
------ DEBUG ------
2021-03-19 18:58:05 +00:00
function draw_debug()
-- draw game data
love.graphics.print("time: "..fps_total..", fps: "..fps_draw..", frametime: "..math.floor(fps_dt* 1000).."ms, x: "..math.floor(g3d.camera.position[1])..", y: "..math.floor(g3d.camera.position[2])..", z: "..math.floor(g3d.camera.position[3]), x, y)
-- draw selected entities data (scrolleable?!)
uc = 0
for _, u in pairs(current_level.units) do for _, entity in pairs(current_level.entities) do
if entity.unit ~= nil and entity.unit == u.id then
uc = uc + 1
if entity.model ~= nil then
if entity.is_animated and entity.is_selected == true then
love.graphics.setColor(0,0,0,0.3)
love.graphics.rectangle("fill",15,-85+80*uc+scroll,game_width-30,70)
love.graphics.setColor(1,1,1)
love.graphics.print("["..u.name..", model: "..entity.name.." (id: "..entity.num..")] (level id: "..entity.tnum..")",20,-80+80*uc+scroll)
love.graphics.print("frame: "..entity.anim_frame.."/"..entity.anim_frames..", rm: \""..entity.rotate_mode.."\"",40,-60+80*uc+scroll)
love.graphics.print("animation: "..entity.anim_path,40,-40+80*uc+scroll)
end
end
end
end end
2021-03-19 18:58:05 +00:00
end
2021-03-20 10:17:05 +00:00
function draw_pause_menu(w,h)
2021-03-18 16:28:10 +00:00
love.graphics.setColor(1,1,1,0.3)
2021-03-20 10:17:05 +00:00
love.graphics.rectangle("fill", w, h, game_width-2*w, game_height-2*h)
2021-03-18 16:28:10 +00:00
love.graphics.setColor(1,1,1,1)
end
2021-03-20 10:17:05 +00:00
--------------------
2021-03-18 16:28:10 +00:00
2021-03-20 10:17:05 +00:00
------ MODELS ------
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 + 30*fps_dt
2021-03-19 18:58:05 +00:00
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
if entity.shadow ~= nil then
entity.shadow:draw()
end
if entity.is_selected ~= nil and entity.is_selected ~= false and entity.selected_model ~= nil then
entity.selected_model:draw()
end
2021-03-19 18:58:05 +00:00
entity.model:draw()
else -- simple loaded entities dont require so much fuss
entity:draw()
end
end
2021-03-20 10:17:05 +00:00
--------------------
2021-03-19 18:58:05 +00:00