summaryrefslogtreecommitdiff
path: root/client/main.lua
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-02-20 03:55:51 +0000
committerubq323 <ubq323@ubq323.website>2023-02-20 03:55:51 +0000
commita3af144dec34adf9e8ac047dd368bb102de40821 (patch)
tree23f84d753dd3158d65f58004686687977f6f5496 /client/main.lua
parent12972093a8eb1853c4bbe32750e43ec390cfe8c5 (diff)
chat system, some controls changes
Diffstat (limited to 'client/main.lua')
-rw-r--r--client/main.lua149
1 files changed, 126 insertions, 23 deletions
diff --git a/client/main.lua b/client/main.lua
index 27b93f9..9de4290 100644
--- a/client/main.lua
+++ b/client/main.lua
@@ -1,5 +1,6 @@
local enet = require"enet"
local json = require"common.dkjson"
+local utf8 = require"utf8"
local SERVER_HOSTNAME = "ubq323.website"
@@ -16,6 +17,7 @@ local Chunk = require"common.chunk".Chunk
local util = require"util"
local Map = require"common.map".Map
local movement = require"movement"
+local msgbox = require"msgbox"
-- local pprint=require"common.pprint"
-- pprint.setup{show_all=true}
@@ -23,11 +25,21 @@ local movement = require"movement"
local help_text = [[
controls:
-wasd: move left mouse: place
-q,e: zoom in/out right mouse: destroy
+wasd: move
shift: sprint
-F3: toggle debug
-F1: show/hide this help]]
+
+left mouse: place
+right mouse: destroy
+mousewheel: zoom in/out
+
+F1: show/hide this help
+F3: show/hide debug
+F11: toggle "big mode" (bad)
+
+enter: toggle chat]]
+
+
+
math.randomseed(os.time())
@@ -35,13 +47,62 @@ local map = Map:make()
local host,peer
+
+local selected_tile = 9
+
+-- normal: regular gameplay
+-- chat: chat box is open
+-- more modes may come
+local ui_mode = "normal"
+
_G.debugmode = false
-local show_controls = true
+local show_controls = false
+
+local this_chatmsg = ""
+local chatmsg_text = love.graphics.newText(love.graphics.getFont())
+
+local big_mode = false
+local orig_w,orig_h = love.graphics.getDimensions()
function love.keypressed(key,scancode,isrepeat)
- if scancode == "f3" then _G.debugmode = not _G.debugmode end
- if scancode == "f1" then show_controls = not show_controls end
+ if key == "f11" then
+ big_mode = not big_mode
+ if big_mode then
+ love.window.setMode(orig_w*2,orig_h*2)
+ else
+ love.window.setMode(orig_w,orig_h)
+ end
+ end
+ if ui_mode == "normal" then
+ if key == "f3" then _G.debugmode = not _G.debugmode end
+ if key == "f1" then show_controls = not show_controls end
+ for i = 1,9 do if key == tostring(i) then selected_tile = i end end
+ if key == "return" then
+ ui_mode = "chat"
+ this_chatmsg = ""
+ end
+ elseif ui_mode == "chat" then
+ if key == "return" then
+ ui_mode = "normal"
+ peer:send(json.encode{t="chat",msg=this_chatmsg})
+ -- msgbox.add("[me] "..this_chatmsg)
+ elseif key == "escape" then
+ ui_mode = "normal"
+ elseif key == "backspace" then
+ local boffs = utf8.offset(this_chatmsg,-1)
+ if boffs then
+ this_chatmsg = this_chatmsg:sub(1,boffs-1)
+ end
+
+ end
+ end
+end
+
+function love.textinput(text)
+ if ui_mode == "chat" then
+ this_chatmsg = this_chatmsg..text
+ end
end
local function draw_player(pl,islocal)
@@ -61,7 +122,7 @@ local remote_players = {}
local function update_local_player(pl,dt)
local SPEED = 8*math.sqrt(3) -- 8 hexagonheights per second
- if love.keyboard.isScancodeDown("lshift") then SPEED = SPEED*2 end
+ if love.keyboard.isDown("lshift") then SPEED = SPEED*2.5 end
local function kd(code)
if love.keyboard.isScancodeDown(code) then return 1 else return 0 end
end
@@ -96,25 +157,35 @@ local function send_settile(hpos,tile)
peer:send(json.encode{t="settile",q=hpos.q,r=hpos.r,tile=tile})
end
+function love.wheelmoved(dx,dy)
+ camera.zoom = camera.zoom * (1.15 ^ dy)
+ camera.zoom = math.max(2.5,math.min(50,camera.zoom))
+end
function love.update(dt)
- if local_player then
- update_local_player(local_player,dt)
- if love.keyboard.isScancodeDown"q" then camera.zoom = camera.zoom*1.05 end
- if love.keyboard.isScancodeDown"e" then camera.zoom = camera.zoom/1.05 end
- camera.zoom = math.max(2.5,math.min(50,camera.zoom))
- sync_local_player(local_player)
- end
+ msgbox.update(dt)
+ if ui_mode == "normal" then
+
+ -- movement and zoom in/out (keyboard input)
+ if local_player then
+ update_local_player(local_player,dt)
+ sync_local_player(local_player)
+ end
- local mh = camera:screen_to_world(Pos:make(love.mouse.getPosition())):to_hex():round()
- if map:at(mh) == 0 and love.mouse.isDown(1) then
- map:set_at(mh,9)
- send_settile(mh,9)
- elseif map:at(mh) ~= 0 and love.mouse.isDown(2) then
- map:set_at(mh,0)
- send_settile(mh,0)
+ -- mouse input
+ local msx,msy = love.mouse.getPosition()
+ if big_mode then msx,msy = msx/2,msy/2 end
+ local mh = camera:screen_to_world(Pos:make(msx,msy)):to_hex():round()
+ if map:at(mh) == 0 and love.mouse.isDown(1) then
+ map:set_at(mh,selected_tile)
+ send_settile(mh,selected_tile)
+ elseif map:at(mh) ~= 0 and love.mouse.isDown(2) then
+ map:set_at(mh,0)
+ send_settile(mh,0)
+ end
end
+ -- load and unload chunks
if local_player then
local player_cp = local_player.pos:to_hex():containing_chunk()
-- load chunks near to player (within 3x3 square)
@@ -140,6 +211,7 @@ function love.update(dt)
end
+ -- handle network packets
repeat
local ev = host:service()
if ev and ev.type == "receive" then
@@ -149,9 +221,11 @@ function love.update(dt)
if op == "join" then
local pl = j.pl
remote_players[pl.id] = {pos=coords.Pos:make(pl.x,pl.y),color=pl.color,id=pl.id}
+ msgbox.add(pl.id.." joined")
elseif op == "leave" then
local id = j.id
remote_players[id]=nil
+ msgbox.add(id.." left")
elseif op == "move" then
local id,x,y = j.id,j.x,j.y
assert(remote_players[id],"wheeze "..id)
@@ -166,6 +240,9 @@ function love.update(dt)
elseif op == "settile" then
local h = coords.Hex:make(j.q,j.r)
map:set_at(h,j.tile)
+ elseif op == "chat" then
+ local msg,from = j.msg,j.from
+ msgbox.add("["..tostring(from).."] "..msg)
end
end
until not ev
@@ -174,6 +251,7 @@ end
function love.draw()
love.graphics.clear(1,1,1)
love.graphics.origin()
+ if big_mode then love.graphics.scale(2) end
if local_player then
camera.pos = local_player.pos
end
@@ -196,6 +274,8 @@ function love.draw()
local hm = wm:to_hex()
love.graphics.origin()
+ if big_mode then love.graphics.scale(2) end
+ util.print_good(tostring(selected_tile), 400,10)
if _G.debugmode and local_player then
util.print_good({
"ms "..tostring(sm),
@@ -208,19 +288,42 @@ function love.draw()
"voob "..tostring(camera.zoom),
"-",
"fps "..tostring(love.timer.getFPS()),
- "ping "..tostring(peer:round_trip_time())
+ "ping "..tostring(peer:round_trip_time()),
},10,10)
end
if show_controls then
util.print_good(help_text,300,200)
end
+
+ msgbox.draw()
+
+ if ui_mode ~= "normal" then
+ util.print_good(ui_mode, 700,10)
+ end
+
+ if ui_mode == "chat" then
+ local W,H = love.graphics.getDimensions()
+ chatmsg_text:set("- "..this_chatmsg)
+ local tw,th = chatmsg_text:getDimensions()
+ local y = H-th-30
+ love.graphics.setColor(0,0,0,0.8)
+ love.graphics.rectangle("fill",0,y,W,th)
+ love.graphics.setColor(1,1,1)
+ love.graphics.draw(chatmsg_text,0,y)
+ love.graphics.setColor(0.8,0.8,0.8)
+ love.graphics.line(tw,y,tw,y+th)
+ end
+
end
function love.load()
+ love.keyboard.setKeyRepeat(true)
-- require"profile".start(10,io.open("./trace","w"))
host = enet.host_create()
peer = host:connect(SERVER_HOSTNAME..":8473")
+ msgbox.add("connected to "..SERVER_HOSTNAME..":8473")
+ msgbox.add("press F1 for controls help")
end
function love.quit()