A_RTS/main.lua

270 lines
6.7 KiB
Lua

-- load 3d engine
g3d = require("g3d")
function love.load()
-- GRAPHICS
-- GOOD PIXEL
love.graphics.setDefaultFilter("nearest")
-- FONTS
DefaultFont = love.graphics.newImageFont("assets/default_font.png",
" 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
speed = 0
-- debug
scroll = 0
debug = false
-- player
player = {
id = 1
}
-- enums
require "scripts/enums"
-- functions
require "scripts/drawing_UI"
--objects
require "scripts/objects"
require "scripts/units"
-- levels
require "scripts/levels"
current_level = levels.main_menu
end
function love.update(dt)
-- 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
end
fps_second = fps_second + dt
fps_dt = dt
fps_count = fps_count + 1
-- camera: slow, fast, medium?
speed = 8
-- do camera
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
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
function love.wheelmoved(_, y)
if debug then
scroll = scroll+15*y
else
-- 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
end
function love.keypressed(key)
-- pause the game
if key == "escape" then
if game_pause then
game_pause = false
else
game_pause = true
end
end
-- select all units button
if key == "f2" then
if has_troops_selected then
has_troops_selected = false
else
has_troops_selected = true
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
end
end
function love.draw()
-- get drawing globals
game_width = love.graphics.getWidth()
game_height = love.graphics.getHeight()
-- LAYER 1: THE WORLD
draw_entity(current_level.skybox)
-- 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
end
-- 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)
end
-- debug
if debug then draw_debug() end
end
------ DEBUG ------
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
end
function draw_pause_menu(w,h)
love.graphics.setColor(1,1,1,0.3)
love.graphics.rectangle("fill", w, h, game_width-2*w, game_height-2*h)
love.graphics.setColor(1,1,1,1)
end
--------------------
------ MODELS ------
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
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
entity.model:draw()
else -- simple loaded entities dont require so much fuss
entity:draw()
end
end
--------------------