A_RTS/main.lua

176 lines
4.2 KiB
Lua

g3d = require "g3d"
function love.load()
-- GRAPHICS
-- GOOD PIXEL
love.graphics.setDefaultFilter("nearest")
-- FONTS
DefaultFont = love.graphics.newImageFont("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
-- enums
require "enums"
--objects
require "objects"
-- levels
require "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?
if love.keyboard.isDown("lshift") then
speed = 2
elseif love.keyboard.isDown("lctrl") then
speed = 6
else
speed = 4
end
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
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
function love.wheelmoved(_, y)
g3d.camera.zoom = math.min(math.max(3,g3d.camera.zoom-y/1.5),30)
is_scrolling = true
end
function love.keypressed(key)
-- pause the game and free the mouse
if key == "escape" then
if game_pause then
game_pause = false
else
game_pause = true
end
end
end
function love.draw()
-- get drawing globals
game_width = love.graphics.getWidth()
game_height = love.graphics.getHeight()
-- draw every model
local ent_count = 0
for _, entity in pairs(current_level.entities) do
ent_count = ent_count + 1
if entity.model ~= nil then
-- do animated models
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 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
entity:draw()
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
-- print coords
if game_pause then
draw_pause_menu(30,30)
else
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
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)
end
end
end
end
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