Remove channel polling for input

This commit is contained in:
giongto35 2019-04-23 03:22:53 +08:00
parent 5b20cfb694
commit 4b98bc84e1
3 changed files with 21 additions and 20 deletions

View file

@ -44,9 +44,9 @@ func (d *Director) SetView(view *GameView) {
d.timestamp = float64(time.Now().Nanosecond()) / float64(time.Second)
}
func (d *Director) UpdateInput(input int) {
d.view.UpdateInput(input)
}
//func (d *Director) UpdateInput(input int) {
//d.view.UpdateInput(input)
//}
func (d *Director) Step() {
timestamp := float64(time.Now().Nanosecond()) / float64(time.Second)
@ -69,11 +69,11 @@ func (d *Director) Run() {
L:
for range c {
// quit game
// TODO: Anyway not using select because it will slow down
select {
// if there is event from close channel => the game is ended
case input := <-d.inputChannel:
d.UpdateInput(input)
//case input := <-d.inputChannel:
//d.UpdateInput(input)
case <-d.Done:
break L
default:

View file

@ -54,29 +54,30 @@ func NewGameView(console *nes.Console, title, hash string, imageChannel chan *im
inputChannel: inputChannel,
}
go gameview.ListenToInputChannel()
return gameview
}
// ListenToInputChannel listen from input channel streamm, which is exposed to WebRTC session
func (view *GameView) UpdateInput(keysInBinary int) {
for i := 0; i < NumKeys*2; i++ {
b := ((keysInBinary & 1) == 1)
view.keyPressed[i] = (view.keyPressed[i] && b) || b
keysInBinary = keysInBinary >> 1
}
}
// ListenToInputChannel listen from input channel streamm, which is exposed to WebRTC session
//func (view *GameView) ListenToInputChannel() {
//for {
//keysInBinary := <-view.inputChannel
//func (view *GameView) UpdateInput(keysInBinary int) {
//for i := 0; i < NumKeys*2; i++ {
//b := ((keysInBinary & 1) == 1)
//view.keyPressed[i] = (view.keyPressed[i] && b) || b
//keysInBinary = keysInBinary >> 1
//}
//}
//}
// ListenToInputChannel listen from input channel streamm, which is exposed to WebRTC session
func (view *GameView) ListenToInputChannel() {
for {
keysInBinary := <-view.inputChannel
for i := 0; i < NumKeys*2; i++ {
b := ((keysInBinary & 1) == 1)
view.keyPressed[i] = (view.keyPressed[i] && b) || b
keysInBinary = keysInBinary >> 1
}
}
}
// Enter enter the game view.
func (view *GameView) Enter() {

2
ws.go
View file

@ -117,7 +117,7 @@ func (c *Client) heartbeat() {
func (c *Client) listen() {
for {
log.Println("Waiting for message ...")
//log.Println("Waiting for message ...")
_, rawMsg, err := c.conn.ReadMessage()
if err != nil {
log.Println("[!] read:", err)