Skip to content

Commit 492127c

Browse files
committed
Update events handling
1 parent 78bf882 commit 492127c

File tree

10 files changed

+340
-313
lines changed

10 files changed

+340
-313
lines changed

coffee/client/module/Chat.coffee

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Chat
2+
constructor:(options)->
3+
_this=@
4+
@FPC=options.FPC
5+
@chatDiv = document.querySelector(".chat")
6+
@listen()
7+
return
8+
listen:()->
9+
_this=@
10+
window.addEventListener "wheel", (e)->
11+
if _this.FPC.gameState isnt "chat"
12+
e.preventDefault()
13+
return
14+
, {passive: false}
15+
return @
16+
isElementScrolledToBottom:(el)->
17+
if el.scrollTop >= (el.scrollHeight - el.offsetHeight)
18+
return true
19+
return false
20+
scrollToBottom:(el)->
21+
el.scrollTop = el.scrollHeight
22+
return
23+
log:(message)->
24+
atBottom = @isElementScrolledToBottom(@chatDiv)
25+
$(".chat").append(message+"<br>")
26+
if atBottom
27+
@scrollToBottom(@chatDiv)
28+
return
29+
export {Chat}

coffee/client/module/FirstPersonControls.coffee

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ class FirstPersonControls
105105
@gameState=state
106106
console.log "Game state: "+state
107107
resetState:()->
108-
$(".gameMenu").hide()
109108
$(".chat").removeClass("focus")
110109
$(".chat").addClass("blur")
111110
$(".com_i").blur()
@@ -119,6 +118,7 @@ class FirstPersonControls
119118
@reqLock()
120119
when "gameLock"
121120
@state "gameLock"
121+
$(".gameMenu").hide()
122122
when "menu"
123123
@state "menu"
124124
$(".gameMenu").show()
@@ -127,12 +127,14 @@ class FirstPersonControls
127127
if @gameState is "gameLock"
128128
$(".chat").addClass("focus")
129129
$(".chat").removeClass("blur")
130+
$(".gameMenu").hide()
130131
@state "chat"
131132
@unLock()
132133
$(".com").show()
133134
$(".com_i").focus()
134135
when "inventory"
135136
if @gameState isnt "menu"
137+
$(".gameMenu").hide()
136138
if @gameState isnt "inventory"
137139
@state "inventory"
138140
$(".inv_window").show()

coffee/client/module/InventoryBar.coffee

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,4 @@ class InventoryBar
3232
setXp: (level,progress)->
3333
$(".player_xp").text level
3434
$(".xp_bar").css "width","#{500*progress}px"
35-
listen: ->
36-
_this=@
37-
# $(window).on 'wheel', (event) ->
38-
# if event.originalEvent.deltaY < 0
39-
# _this.moveBoxPlus()
40-
# else
41-
# _this.moveBoxMinus()
42-
# $(document).keydown (z) ->
43-
# _this.directBoxChange(z)
44-
return @
4535
export {InventoryBar}

coffee/client/module/index.coffee

Lines changed: 63 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {AssetLoader} from './AssetLoader.js'
1010
import {InventoryBar} from './InventoryBar.js'
1111
import {RandomNick} from './RandomNick.js'
1212
import {GUI} from './jsm/libs/dat.gui.module.js'
13+
import {Chat} from './Chat.js'
1314

1415
init = ()->
1516
#Płótno,renderer,scena i kamera
@@ -52,70 +53,76 @@ init = ()->
5253
al
5354
})
5455

55-
#komunikacja z serwerem websocket
56+
#Połączenie z serwerem i kontrolki gracza
5657
socket=io.connect "#{al.get("host")}:#{al.get("websocket-port")}"
57-
socket.on "connect",()->
58-
console.log "Połączono z serverem!"
59-
$('.loadingText').text "Za chwilę dołączysz do gry..."
60-
nick=document.location.hash.substring(1,document.location.hash.length)
61-
if nick is ""
62-
nick=RandomNick()
63-
document.location.href="\##{nick}"
64-
console.log "User nick: #{nick}"
65-
socket.emit "initClient", {
66-
nick:nick
67-
}
68-
return
69-
socket.on "blockUpdate",(block)->
70-
world.setBlock block[0],block[1]+16,block[2],block[3]
71-
return
72-
socket.on "spawn", (yaw,pitch)->
73-
console.log "Gracz dołączył do gry!"
74-
$(".initLoading").css "display","none"
75-
camera.rotation.y=yaw
76-
camera.rotation.x=pitch
77-
socket.on "mapChunk", (sections,x,z,biomes)->
78-
world._computeSections sections,x,z,biomes
79-
socket.on "hp",(points)->
80-
inv_bar.setHp(points)
81-
socket.on "inventory",(inv)->
82-
console.log inv
83-
socket.on "food",(points)->
84-
inv_bar.setFood(points)
85-
socket.on "msg",(msg)->
86-
atBottom = isElementScrolledToBottom(consoleDiv)
87-
$(".chat").append(msg+"<br>")
88-
if atBottom
89-
scrollToBottom(consoleDiv)
90-
socket.on "kicked",(reason)->
91-
atBottom = isElementScrolledToBottom(consoleDiv)
92-
$(".chat").append("You have been kicked!<br>")
93-
if atBottom
94-
scrollToBottom(consoleDiv)
95-
socket.on "xp",(xp)->
96-
inv_bar.setXp xp.level,xp.progress
97-
socket.on "move", (pos)->
98-
to={x:pos.x-0.5,y:pos.y+17,z:pos.z-0.5}
99-
new TWEEN.Tween camera.position
100-
.to to, 100
101-
.easing TWEEN.Easing.Quadratic.Out
102-
.start()
103-
104-
#Utworzenie inventory
105-
inv_bar = new InventoryBar({
106-
boxSize: 60
107-
padding: 4
108-
div: ".inventoryBar"
109-
}).listen()
110-
111-
#Kontrolki gracza
11258
FPC = new FirstPersonControls {
11359
canvas
11460
camera
11561
micromove: 0.3
11662
socket
11763
}
11864

65+
#Czat
66+
chat=new Chat({FPC})
67+
68+
#Komunikacja z serwerem websocket
69+
eventMap={
70+
"connect":()->
71+
console.log "Połączono z serverem!"
72+
$('.loadingText').text "Za chwilę dołączysz do gry..."
73+
nick=document.location.hash.substring(1,document.location.hash.length)
74+
if nick is ""
75+
nick=RandomNick()
76+
document.location.href="\##{nick}"
77+
console.log "User nick: #{nick}"
78+
socket.emit "initClient", {
79+
nick:nick
80+
}
81+
return
82+
"blockUpdate":(block)->
83+
world.setBlock block[0],block[1]+16,block[2],block[3]
84+
return
85+
"spawn":(yaw,pitch)->
86+
console.log "Gracz dołączył do gry!"
87+
$(".initLoading").css "display","none"
88+
camera.rotation.y=yaw
89+
camera.rotation.x=pitch
90+
return
91+
"mapChunk":(sections,x,z,biomes)->
92+
world._computeSections sections,x,z,biomes
93+
return
94+
"hp":(points)->
95+
inv_bar.setHp(points)
96+
return
97+
"inventory":(inv)->
98+
console.log inv
99+
return
100+
"food":(points)->
101+
inv_bar.setFood(points)
102+
return
103+
"msg":(msg)->
104+
chat.log(msg)
105+
return
106+
"kicked":(reason)->
107+
chat.log("You have been kicked!")
108+
return
109+
"xp":(xp)->
110+
inv_bar.setXp xp.level,xp.progress
111+
return
112+
"move":(pos)->
113+
to={x:pos.x-0.5,y:pos.y+17,z:pos.z-0.5}
114+
new TWEEN.Tween camera.position
115+
.to to, 100
116+
.easing TWEEN.Easing.Quadratic.Out
117+
.start()
118+
return
119+
}
120+
for i of eventMap
121+
socket.on i,eventMap[i]
122+
123+
#Utworzenie inventory
124+
inv_bar = new InventoryBar()
125+
119126
#Kursor raycastowania
120127
cursor=new THREE.LineSegments(
121128
new THREE.EdgesGeometry(
@@ -148,19 +155,6 @@ init = ()->
148155
gui.add( world.material, 'wireframe' ).name( 'Wireframe' ).listen()
149156
gui.add( params, 'chunkdist',0,10,1).name( 'Render distance' ).listen()
150157

151-
#Autoscrollowanie chatu
152-
consoleDiv = document.querySelector(".chat")
153-
154-
window.addEventListener "wheel", (e)->
155-
if FPC.gameState isnt "chat"
156-
e.preventDefault()
157-
, {passive: false}
158-
isElementScrolledToBottom=(el)->
159-
if el.scrollTop >= (el.scrollHeight - el.offsetHeight)
160-
return true
161-
return false
162-
scrollToBottom=(el)->
163-
el.scrollTop = el.scrollHeight
164158
#Wprawienie w ruch funkcji animate
165159
animate()
166160
return

coffee/server.coffee

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ module.exports=(type)->
1414
Convert = require 'ansi-to-html'
1515
convert = new Convert()
1616

17-
18-
1917
opn("http://#{config.host}:#{config['express-port']}")
2018

2119
#początkowe zmienne
@@ -40,6 +38,8 @@ module.exports=(type)->
4038

4139
#websocket
4240
io.sockets.on "connection", (socket)->
41+
socketInfo[socket.id]={}
42+
bot=socketInfo[socket.id]
4343
socket.on "initClient",(data)->
4444
console.log "[+] "+data.nick
4545

@@ -51,71 +51,67 @@ module.exports=(type)->
5151
username: socketInfo[socket.id].nick
5252
version: config.realServer.version
5353
}
54-
54+
bot=()->
55+
return socketInfo[socket.id].bot
56+
emit=(array)->
57+
io.to(socket.id).emit array...
5558
#Eventy otrzymywane z serwera minecraftowego
56-
socketInfo[socket.id].bot._client.on "map_chunk",(packet)->
59+
bot()._client.on "map_chunk",(packet)->
5760
cell=new Chunk()
5861
cell.load packet.chunkData,packet.bitMap,false,true
59-
io.to(socket.id).emit "mapChunk", cell.sections,packet.x,packet.z,packet.biomes
62+
emit ["mapChunk", cell.sections,packet.x,packet.z,packet.biomes]
6063
return
61-
socketInfo[socket.id].bot.on 'chat',(username, message)->
62-
if username is socketInfo[socket.id].bot.username
64+
botEventMap={
65+
"move":()->
66+
emit ["move",bot().entity.position]
6367
return
64-
return
65-
socketInfo[socket.id].bot.on 'move',()->
66-
try
67-
io.to(socket.id).emit "move",socketInfo[socket.id].bot.entity.position
68-
return
69-
socketInfo[socket.id].bot.on 'health',()->
70-
try
71-
io.to(socket.id).emit "hp",socketInfo[socket.id].bot.health
72-
io.to(socket.id).emit "food",socketInfo[socket.id].bot.food
73-
return
68+
"health":()->
69+
emit ["hp",bot().health]
70+
emit ["food",bot().food]
71+
return
72+
"spawn":()->
73+
emit ["spawn",bot().entity.yaw,bot().entity.pitch]
74+
return
75+
"kicked":(reason,loggedIn)->
76+
emit ["kicked",reason]
77+
return
78+
"message":(msg)->
79+
emit ["msg",convert.toHtml(msg.toAnsi())]
80+
return
81+
"experience":()->
82+
emit ["xp",bot().experience]
83+
return
84+
"blockUpdate":(oldb,newb)->
85+
emit ["blockUpdate",[newb.position.x,newb.position.y,newb.position.z,newb.stateId]]
86+
return
87+
}
88+
for i of botEventMap
89+
socketInfo[socket.id].bot.on i, botEventMap[i]
90+
7491
inv=""
7592
socketInfo[socket.id].int=setInterval ()->
76-
inv_new=JSON.stringify(socketInfo[socket.id].bot.inventory.slots)
93+
inv_new=JSON.stringify(bot().inventory.slots)
7794
if inv isnt inv_new
7895
inv=inv_new
79-
io.to(socket.id).emit "inventory",socketInfo[socket.id].bot.inventory.slots
96+
emit "inventory",bot().inventory.slots
8097
,100
81-
socketInfo[socket.id].bot.on 'spawn',()->
82-
try
83-
io.to(socket.id).emit "spawn",socketInfo[socket.id].bot.entity.yaw,socketInfo[socket.id].bot.entity.pitch
84-
return
85-
socketInfo[socket.id].bot.on 'kicked',(reason,loggedIn)->
86-
try
87-
io.to(socket.id).emit "kicked",reason
88-
return
89-
socketInfo[socket.id].bot.on 'message',(msg)->
90-
try
91-
io.to(socket.id).emit "msg",convert.toHtml(msg.toAnsi());
92-
return
93-
socketInfo[socket.id].bot.on 'experience',()->
94-
try
95-
io.to(socket.id).emit "xp",socketInfo[socket.id].bot.experience
96-
return
97-
socketInfo[socket.id].bot.on 'blockUpdate',(oldb,newb)->
98-
io.to(socket.id).emit "blockUpdate",[newb.position.x,newb.position.y,newb.position.z,newb.stateId]
99-
return
100-
return
101-
102-
#eventy otrzymywane od klienta
103-
socket.on "move",(state,toggle)->
104-
try
105-
socketInfo[socket.id].bot.setControlState(state,toggle)
106-
return
107-
socket.on "command",(com)->
108-
try
109-
socketInfo[socket.id].bot.chat(com)
110-
return
111-
socket.on "rotate",(data)->
112-
try
113-
socketInfo[socket.id].bot.look data...
114-
return
115-
socket.on "disconnect", ->
116-
try
117-
clearInterval socketInfo[socket.id].int
118-
console.log "[-] "+socketInfo[socket.id].nick
119-
socketInfo[socket.id].bot.end()
120-
delete socketInfo[socket.id]
121-
return
98+
socketEventMap={
99+
"move":(state,toggle)->
100+
bot().setControlState(state,toggle)
101+
return
102+
"command":(com)->
103+
bot().chat(com)
104+
return
105+
"rotate":(data)->
106+
bot().look data...
107+
return
108+
"disconnect":()->
109+
try
110+
clearInterval socketInfo[socket.id].int
111+
console.log "[-] "+socketInfo[socket.id].nick
112+
socketInfo[socket.id].bot.end()
113+
delete socketInfo[socket.id]
114+
return
115+
}
116+
for i of socketEventMap
117+
socket.on i,socketEventMap[i]

0 commit comments

Comments
 (0)