~sircmpwn/cells

a0c7f20043108cdef497538b2b4b0ce6724d69eb — Drew DeVault 7 months ago 2816bf2
Implement map storage and updates

MVP!
4 files changed, 82 insertions(+), 9 deletions(-)

M conn.go
M index.js
A map.go
M messages.go
M conn.go => conn.go +17 -0
@@ 46,6 46,15 @@ func (c *Connection) Run() {
		return true
	})

	// TODO: Only send proximate chunks
	for coords, chunk := range Map {
		c.Conn.WriteJSON(&ChunkDataMessage{
			Message: Message{"chunk_data"},
			Coords:  coords,
			Cells:   *chunk,
		})
	}

	for {
		_, data, err := c.Conn.ReadMessage()
		if err != nil {


@@ 72,6 81,14 @@ func (c *Connection) Run() {
			c.Player.Position = motion.Position
			motion.PlayerId = c.Player.Id
			c.SendToPeers(&motion)
		case "set_cell":
			// TODO: Enforce reach distance
			var setCell SetCellMessage
			json.Unmarshal(data, &setCell)
			SetCell(setCell.Coords, c.Player.Color)
			setCell.Color = c.Player.Color
			c.SendToPeers(&setCell)
			break;
		}
	}


M index.js => index.js +12 -3
@@ 97,8 97,11 @@ function set_cell({ x, y }, color) {
    local_y = CHUNK_SIZE + local_y;
  }
  if (typeof color === "undefined") {
    // TODO: Send change to server
    color = player_color;
    ws.send(JSON.stringify({
      "type": "set_cell",
      "coords": { x, y },
    }));
  }
  map[key][local_y * CHUNK_SIZE + local_x] = color;
}


@@ 380,6 383,9 @@ ws.addEventListener("message", e => {
  case "spawn_peer":
    peers.push(data.peer);
    break;
  case "despawn_peer":
    peers = peers.filter(peer => peer.id != data.peer_id);
    break;
  case "ping":
    ping(data.coords, data.color);
    break;


@@ 391,8 397,11 @@ ws.addEventListener("message", e => {
      }
    }
    break;
  case "despawn_peer":
    peers = peers.filter(peer => peer.id != data.peer_id);
  case "chunk_data":
    map[`${data.coords.x},${data.coords.y}`] = data.cells;
    break;
  case "set_cell":
    set_cell(data.coords, data.color);
    break;
  }
});

A map.go => map.go +41 -0
@@ 0,0 1,41 @@
package main

var Map map[Coordinates]*[CHUNK_SIZE*CHUNK_SIZE]Color

func init() {
	Map = make(map[Coordinates]*[CHUNK_SIZE*CHUNK_SIZE]Color)
}

func SetCell(coords Coordinates, c Color) {
	// TODO: Lock mutex
	loc := Coordinates{
		X: coords.X / CHUNK_SIZE,
		Y: coords.Y / CHUNK_SIZE,
	}
	if coords.X < 0 {
		loc.X--
	}
	if coords.Y < 0 {
		loc.Y--
	}
	chunk, ok := Map[loc]
	if !ok {
		var blank [CHUNK_SIZE*CHUNK_SIZE]Color
		for n := 0; n < len(blank); n += 1 {
			blank[n] = Color{255, 255, 255};
		}
		chunk = &blank
		Map[loc] = chunk
	}
	local := Coordinates{
		X: coords.X % CHUNK_SIZE,
		Y: coords.Y % CHUNK_SIZE,
	}
	if local.X < 0 {
		local.X = CHUNK_SIZE + local.X
	}
	if local.Y < 0 {
		local.Y = CHUNK_SIZE + local.Y
	}
	chunk[local.Y * CHUNK_SIZE + local.X] = c
}

M messages.go => messages.go +12 -6
@@ 39,12 39,6 @@ type DespawnPeerMessage struct {
	PeerId int64 `json:"peer_id"`
}

type CellMapMessage struct {
	Message
	Coords Coordinates                    `json:"coords"`
	Cells  [CHUNK_SIZE * CHUNK_SIZE]Color `json:"cells"`
}

type PingMessage struct {
	Message
	Coords Coordinates `json:"coords"`


@@ 56,3 50,15 @@ type PlayerMotionMessage struct {
	PlayerId int64    `json:"player_id"`
	Position Position `json:"position"`
}

type ChunkDataMessage struct {
	Message
	Coords Coordinates                    `json:"coords"`
	Cells  [CHUNK_SIZE * CHUNK_SIZE]Color `json:"cells"`
}

type SetCellMessage struct {
	Message
	Coords Coordinates `json:"coords"`
	Color  Color       `json:"color"`
}