This commit is contained in:
UndeadMaelys 2021-04-18 21:46:29 +02:00
parent e1384e7729
commit dd91dd450c
1 changed files with 52 additions and 51 deletions

103
main.lua
View File

@ -1,4 +1,5 @@
g3d = require "g3d" -- load 3d engine
g3d = require("g3d")
@ -12,39 +13,39 @@ function love.load()
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0" .. "ABCDEFGHIJKLMNOPQRSTUVWXYZ0" ..
"123456789.,!?-+/():;%&`'*#=[]\"") "123456789.,!?-+/():;%&`'*#=[]\"")
love.graphics.setFont(DefaultFont) love.graphics.setFont(DefaultFont)
--fps_count --fps_count
fps_count = 0 fps_count = 0
fps_second = 0 fps_second = 0
fps_draw = 0 fps_draw = 0
fps_total = 0 fps_total = 0
-- camera -- camera
g3d.camera.pivot = { 0, 0, 0 } g3d.camera.pivot = { 0, 0, 0 }
g3d.camera.position = { 0, 0, 0 } g3d.camera.position = { 0, 0, 0 }
g3d.camera.pitch = 0 g3d.camera.pitch = 0
g3d.camera.zoom = 10 g3d.camera.zoom = 10
speed = 0 speed = 0
-- debug -- debug
scroll = 0 scroll = 0
debug = false debug = false
-- player -- player
player = { player = {
id = 1 id = 1
} }
-- enums -- enums
require "scripts/enums" require "scripts/enums"
-- functions -- functions
require "scripts/drawing_UI" require "scripts/drawing_UI"
--objects --objects
require "scripts/objects" require "scripts/objects"
require "scripts/units" require "scripts/units"
-- levels -- levels
require "scripts/levels" require "scripts/levels"
current_level = levels.main_menu current_level = levels.main_menu
@ -54,35 +55,35 @@ end
function love.update(dt) function love.update(dt)
-- count frames per seconds -- count frames per seconds
if fps_second >= 1 then if fps_second >= 1 then
fps_second = fps_second - 1 fps_second = fps_second - 1
fps_draw = fps_count fps_draw = fps_count
fps_count = 0 fps_count = 0
fps_total = fps_total + 1 fps_total = fps_total + 1
end end
fps_second = fps_second + dt fps_second = fps_second + dt
fps_dt = dt fps_dt = dt
fps_count = fps_count + 1 fps_count = fps_count + 1
-- camera: slow, fast, medium? -- camera: slow, fast, medium?
speed = 8 speed = 8
-- do camera -- do camera
if not game_pause then if not game_pause then
g3d.camera.strategyViewMovement(dt,speed,is_scrolling) g3d.camera.strategyViewMovement(dt,speed,is_scrolling)
local center = -500 local center = -500
local pos = {g3d.camera.position[1],g3d.camera.position[2]+center,g3d.camera.position[3]} local pos = {g3d.camera.position[1],g3d.camera.position[2]+center,g3d.camera.position[3]}
current_level.skybox:setTransform(pos) current_level.skybox:setTransform(pos)
-- movement: w,a,s,d, -- movement: w,a,s,d,
-- rotate: q,e -- rotate: q,e
-- move by screen: 20 px: sides, forward, backwards -- move by screen: 20 px: sides, forward, backwards
-- mouse scroll: zoom -- mouse scroll: zoom
end end
-- select troops -- select troops
if love.mouse.isDown(1) == true then if love.mouse.isDown(1) == true then
if selecting_troops == false then if selecting_troops == false then
sel_pos_x = love.mouse.getX() sel_pos_x = love.mouse.getX()
sel_pos_y = love.mouse.getY() sel_pos_y = love.mouse.getY()
has_troops_selected = false has_troops_selected = false
@ -95,13 +96,13 @@ function love.update(dt)
end end
selecting_troops = false selecting_troops = false
end end
if has_troops_selected then if has_troops_selected then
end end
end end
function love.wheelmoved(_, y) function love.wheelmoved(_, y)
if debug then if debug then
scroll = scroll+15*y scroll = scroll+15*y
else else
@ -113,24 +114,24 @@ end
function love.keypressed(key) function love.keypressed(key)
-- pause the game -- pause the game
if key == "escape" then if key == "escape" then
if game_pause then if game_pause then
game_pause = false game_pause = false
else else
game_pause = true game_pause = true
end end
end end
-- select all units button -- select all units button
if key == "f2" then if key == "f2" then
if has_troops_selected then if has_troops_selected then
has_troops_selected = false has_troops_selected = false
else else
has_troops_selected = true has_troops_selected = true
end end
for _, u in pairs(current_level.units) do for _, entity in pairs(current_level.entities) do 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 entity.unit ~= nil and entity.unit == u.id then
if has_troops_selected then if has_troops_selected then
entity.is_selected = false entity.is_selected = false
entity.selected_model = nil entity.selected_model = nil
@ -139,14 +140,14 @@ function love.keypressed(key)
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}) 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 end end end
end end
if key == "f1" then if key == "f1" then
if debug then debug = false else debug = true end if debug then debug = false else debug = true end
end end
end end
function love.draw() function love.draw()
-- get drawing globals -- get drawing globals
game_width = love.graphics.getWidth() game_width = love.graphics.getWidth()
@ -169,37 +170,38 @@ function love.draw()
-- LAYER 2: THE USER INTERFACE -- LAYER 2: THE USER INTERFACE
if game_pause then if game_pause then
draw_pause_menu(30,30) draw_pause_menu(30,30)
else else
draw_combat_ui() draw_combat_ui()
end end
-- LAYER 3: INPUT & DEBUG -- LAYER 3: INPUT & DEBUG
-- draw selection area -- draw selection area
if selecting_troops == true then 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) love.graphics.rectangle("line",sel_pos_x,sel_pos_y, love.mouse.getX() - sel_pos_x, love.mouse.getY() - sel_pos_y)
end end
-- debug -- debug
if debug then draw_debug() end if debug then draw_debug() end
end end
------ DEBUG ------ ------ DEBUG ------
function draw_debug() function draw_debug()
-- draw game data -- 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) 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?!) -- draw selected entities data (scrolleable?!)
uc = 0 uc = 0
for _, u in pairs(current_level.units) do for _, entity in pairs(current_level.entities) do 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 entity.unit ~= nil and entity.unit == u.id then
uc = uc + 1 uc = uc + 1
if entity.model ~= nil then if entity.model ~= nil then
if entity.is_animated and entity.is_selected == true then if entity.is_animated and entity.is_selected == true then
love.graphics.setColor(0,0,0,0.3) love.graphics.setColor(0,0,0,0.3)
love.graphics.rectangle("fill",15,-85+80*uc+scroll,game_width-30,70) love.graphics.rectangle("fill",15,-85+80*uc+scroll,game_width-30,70)
love.graphics.setColor(1,1,1) 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("["..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("frame: "..entity.anim_frame.."/"..entity.anim_frames..", rm: \""..entity.rotate_mode.."\"",40,-60+80*uc+scroll)
@ -235,34 +237,33 @@ function draw_entity(entity)
-- change -- change
entity.model.mesh:setTexture(entity.anim_imgs[entity.anim_frame]) entity.model.mesh:setTexture(entity.anim_imgs[entity.anim_frame])
end end
-- do rotating models if animated, if has to rotate and if game playing -- 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 if entity.rotate_mode ~= nil and entity.rotate_mode ~= "none" and game_pause ~= true then
local rm = entity.rotate_mode local rm = entity.rotate_mode
if rm == "cam_xz" then if rm == "cam_xz" then
local sin = g3d.camera.position[1]-entity.model.translation[1] local sin = g3d.camera.position[1]-entity.model.translation[1]
local cos = g3d.camera.position[3]-entity.model.translation[3] local cos = g3d.camera.position[3]-entity.model.translation[3]
local angle = math.atan2(sin,cos)-math.rad(180) local angle = math.atan2(sin,cos)-math.rad(180)
entity.model:setRotation(0,angle,0) entity.model:setRotation(0,angle,0)
end end
end end
if entity.shadow ~= nil then if entity.shadow ~= nil then
entity.shadow:draw() entity.shadow:draw()
end end
if entity.is_selected ~= nil and entity.is_selected ~= false and entity.selected_model ~= nil then if entity.is_selected ~= nil and entity.is_selected ~= false and entity.selected_model ~= nil then
entity.selected_model:draw() entity.selected_model:draw()
end end
entity.model:draw() entity.model:draw()
else -- simple loaded entities dont require so much fuss else -- simple loaded entities dont require so much fuss
entity:draw() entity:draw()
end end
end end
-------------------- --------------------