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" ..
"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.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
@ -54,35 +55,35 @@ end
function love.update(dt)
-- count frames per seconds
if fps_second >= 1 then
if fps_second >= 1 then
fps_second = fps_second - 1
fps_draw = fps_count
fps_count = 0
fps_draw = fps_count
fps_count = 0
fps_total = fps_total + 1
end
fps_second = fps_second + dt
fps_dt = 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,
-- 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
if selecting_troops == false then
sel_pos_x = love.mouse.getX()
sel_pos_y = love.mouse.getY()
has_troops_selected = false
@ -95,13 +96,13 @@ function love.update(dt)
end
selecting_troops = false
end
if has_troops_selected then
end
end
function love.wheelmoved(_, y)
function love.wheelmoved(_, y)
if debug then
scroll = scroll+15*y
else
@ -113,24 +114,24 @@ end
function love.keypressed(key)
-- pause the game
if key == "escape" then
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 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 entity.unit ~= nil and entity.unit == u.id then
if has_troops_selected then
entity.is_selected = false
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})
end
end
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()
@ -169,37 +170,38 @@ function love.draw()
-- LAYER 2: THE USER INTERFACE
if game_pause then
draw_pause_menu(30,30)
draw_pause_menu(30,30)
else
draw_combat_ui()
end
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)
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
-- 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.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)
@ -235,34 +237,33 @@ function draw_entity(entity)
-- 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
--------------------