Пример #1
0
func NewPlayer(conn *websocket.Conn) *Player {
	if conn == nil {
		panic("WebSocket connection required")
	}

	outbound := make(chan *events.Event, SOCKET_BUFFER_SIZE)
	outbound_raw := make(chan string, SOCKET_BUFFER_SIZE)
	closing := make(chan bool, 1)

	// Get the region and make it active.
	reg := regions.GetRegion(terrain.WORLD_OVERWORLD, terrain.REGIONTYPE_FIELD, 0, 0)
	// Let the region know to stay alive.
	reg.KeepAlive <- true

	player := Player{
		*performance.NewPerfMixin(),

		conn, reg,
		outbound, outbound_raw, closing,

		entities.NextEntityID(),
		float64(reg.Terrain.Width) / 2, float64(reg.Terrain.Height) / 2, 0, 0, 0, 1,
		time.Now().UnixNano(),
		PLAYER_MAX_HEALTH,
		"",
		nil,
		make([][2]float64, 0),
		false,
		0,
	}
	reg.AddEntity(&player)

	// Set up the player's inventory
	player.inventory = entities.NewInventory(&player, PLAYER_INV_SIZE)
	player.inventory.Give("wsw.sharp.12")
	player.inventory.Give("f5")
	player.inventory.Give("f5")

	go player.startPinging()
	go player.gameTick()

	// Send the player the initial level
	outbound_raw <- "lev{" + reg.String() + "}"

	return &player
}
Пример #2
0
func (self *Player) sendToLocation(parentID, type_ string, x, y int, newX, newY float64) {
	newLocation := regions.GetRegion(parentID, type_, x, y)

	self.x, self.y = newX, newY

	if newLocation == self.location {
		self.location.Broadcast(
			self.location.GetEvent(
				events.ENTITY_UPDATE,
				fmt.Sprintf(
					"{%s}\n%f %f",
					self.PositionString(),
					self.x,
					self.y,
				),
				self,
			),
		)
		self.outbound_raw <- ("epuevt:local\n{" +
			fmt.Sprintf("\"x\":%f,\"y\":%f", newX, newY) +
			"}")
		return
	}

	if newLocation == nil {
		log.Println("Requested region that does not exist:", parentID, type_, x, y)
		return
	}

	if self.location != nil {
		self.location.RemoveEntity(self)
	}

	newLocation.KeepAlive <- true
	self.location = newLocation
	// Send the player the initial level
	self.outbound_raw <- "flv"
	self.outbound_raw <- ("epuevt:local\n{" +
		fmt.Sprintf("\"x\":%f,\"y\":%f", newX, newY) +
		"}")
	newLocation.AddEntity(self)
	self.outbound_raw <- "lev{" + newLocation.String() + "}"
}