Пример #1
0
func (g *Game) join(moniker string) (id string, statech <-chan TurnState,
	err error) {
	g.mtx.Lock()
	defer g.mtx.Unlock()
	if len(g.players) >= g.config.NumPlayers {
		return "", nil, JoinError.New("only %d players allowed",
			g.config.NumPlayers)
	}

	coord, ok := g.randomEmptyCell()
	if !ok {
		panic("grid does not have enough empty cells to place a player!")
	}

	id = newId()
	player := &Player{
		Id:          id,
		Moniker:     moniker,
		Owner:       grid.Owner(len(g.players) + 1),
		Health:      g.config.PlayerHealth,
		Energy:      g.config.PlayerEnergy,
		Coord:       coord,
		Orientation: grid.North,
	}

	g.players = append(g.players, player)

	statech = g.submitAction(player, Join)

	return id, statech, nil
}
Пример #2
0
func loadImages(frames, players int) (
	images map[grid.Cell]*sdl.Surface,
	rotations map[grid.Owner][]*sdl.Surface, err error) {
	images = make(map[grid.Cell]*sdl.Surface)
	for _, simple := range []string{"battery", "floor", "wall"} {
		for _, exploding := range []bool{false, true} {
			data, err := assets.Asset(fmt.Sprintf("final/images/%s.png", simple))
			if err != nil {
				cleanupImages(images)
				return nil, nil, err
			}
			mem := sdl.RWFromMem(unsafe.Pointer(&data[0]), len(data))
			surface, err := img.Load_RW(mem, 0)
			if err != nil {
				cleanupImages(images)
				return nil, nil, err
			}
			if exploding {
				err = overlay(surface, "final/images/ex.png")
				if err != nil {
					surface.Free()
					cleanupImages(images)
					return nil, nil, err
				}
			}
			for orientation := 0; orientation < 4; orientation++ {
				for owner := 0; owner < players+1; owner++ {
					images[grid.Cell{
						Exploding:   exploding,
						Type:        typeMapping(simple),
						Orientation: grid.Orientation(orientation),
						Owner:       grid.Owner(owner)}] = surface
				}
			}
		}
	}
	for _, rotateable := range []string{"l", "p"} {
		for rotations := 0; rotations < 4; rotations++ {
			for player := 1; player <= players; player++ {
				for _, exploding := range []bool{false, true} {
					surface, err := loadImageRotated(fmt.Sprintf("final/images/%s%d.png",
						rotateable, (player-1)%2+1), float64(rotations)/4)
					if err != nil {
						cleanupImages(images)
						return nil, nil, err
					}
					if exploding {
						err = overlay(surface, "final/images/ex.png")
						if err != nil {
							surface.Free()
							cleanupImages(images)
							return nil, nil, err
						}
					}
					images[grid.Cell{
						Exploding:   exploding,
						Type:        typeMapping(rotateable),
						Owner:       grid.Owner(player),
						Orientation: grid.Orientation(rotations)}] = surface
				}
			}
		}
	}

	rotations = make(map[grid.Owner][]*sdl.Surface)
	for _, rotateable := range []string{"p"} {
		for player := 1; player <= players; player++ {
			for frame := 0; frame < 4*frames; frame++ {
				surface, err := loadImageRotated(fmt.Sprintf("final/images/%s%d.png",
					rotateable, (player-1)%2+1), float64(frame)/(4*float64(frames)))
				if err != nil {
					cleanupImages(images)
					cleanupRotations(rotations)
					return nil, nil, err
				}
				rotations[grid.Owner(player)] = append(
					rotations[grid.Owner(player)], surface)
			}
		}
	}

	return images, rotations, nil
}