Exemple #1
0
func NewGameUI(win Window) (*gameUI, error) {
	if err := settings.Settings.Load(); err != nil {
		fmt.Println("cannot load last settings:", err)
	}

	graphics, err := newGraphics()
	if err != nil {
		return nil, err
	}
	g := game.New([]game.Color{game.Red, game.Blue, game.White}, 1)

	// main menu
	size := func(w, h int) rect { return rect{w: w, h: h} }
	newGame := newButton(lang.NewGame, size(500, 80), NewGameOption)
	joinRemoteGame := newButton(lang.JoinRemoteGame, size(500, 80), JoinRemoteGameOption)
	chooseLanguage := newButton(lang.LanguageWord, size(500, 80), ChooseLanguageOption)
	quit := newButton(lang.Quit, size(500, 80), QuitOption)
	mainMenu := newWindow(
		rect{0, 0, gameW, gameH},
		newVerticalFlowLayout(20),
		newGame,
		joinRemoteGame,
		chooseLanguage,
		quit,
	)

	// language menu
	var langBoxes []*checkBox
	for language := lang.Language(0); language < lang.LastLanguage; language++ {
		action := LanguageOptionOffset + int(language)
		cb := newCheckBox(lang.Item(language), size(300, 80), action)
		cb.setChecked(settings.Settings.Language == int(language))
		langBoxes = append(langBoxes, cb)
	}
	languageMenu := newWindow(
		rect{0, 0, gameW, gameH},
		newVerticalFlowLayout(0),
		newCheckBoxGroup(langBoxes...),
		newSpacer(size(0, 20)),
		newButton(lang.OK, size(300, 80), LanguageOKOption),
	)
	languageMenu.setVisible(false)

	// new game menu
	threePlayers := newCheckBox(lang.ThreePlayers, size(350, 80), ThreePlayersOption)
	threePlayers.checked = settings.Settings.PlayerCount == 3
	fourPlayers := newCheckBox(lang.FourPlayers, size(350, 80), FourPlayersOption)
	fourPlayers.checked = settings.Settings.PlayerCount == 4
	var playerMenus [4]*window
	for i := range playerMenus {
		playerIndex := i // need to copy this for use in closures
		nameText := newTextBox(lang.Name, rect{0, 0, 500, 80}, graphics.font)
		nameText.text = settings.Settings.PlayerNames[i]
		nameText.onTextChange(func(text string) {
			settings.Settings.PlayerNames[playerIndex] = text
		})
		playHere := newCheckBox(lang.PlayHere, rect{0, 0, 500, 80}, -1)
		playHere.onCheckChange(func(checked bool) {
			if checked {
				settings.Settings.PlayerTypes[playerIndex] = settings.Human
			}
		})
		playHere.checked = settings.Settings.PlayerTypes[i] == settings.Human
		playAI := newCheckBox(lang.AIPlayer, rect{0, 0, 500, 80}, -1)
		playAI.onCheckChange(func(checked bool) {
			if checked {
				settings.Settings.PlayerTypes[playerIndex] = settings.AI
			}
		})
		playAI.checked = settings.Settings.PlayerTypes[i] == settings.AI
		ipText := newTextBox(lang.IP, rect{0, 0, 500, 80}, graphics.font)
		ipText.text = settings.Settings.IPs[i]
		ipText.onTextChange(func(text string) {
			settings.Settings.IPs[playerIndex] = text
		})
		portText := newTextBox(lang.Port, rect{0, 0, 500, 80}, graphics.font)
		portText.text = settings.Settings.Ports[i]
		portText.onTextChange(func(text string) {
			settings.Settings.Ports[playerIndex] = text
		})
		connectButton := newButton(lang.Connect, size(500, 80), -1)
		playNetwork := newCheckBox(lang.NetworkPlayer, rect{0, 0, 500, 80}, -1)
		playNetwork.onCheckChange(func(checked bool) {
			ipText.setEnabled(checked)
			portText.setEnabled(checked)
			connectButton.setEnabled(checked)
			if checked {
				settings.Settings.PlayerTypes[playerIndex] = settings.NetworkPlayer
			}
		})
		playNetwork.setChecked(settings.Settings.PlayerTypes[i] == settings.NetworkPlayer)

		playerMenus[i] = newWindow(
			rect{},
			newVerticalFlowLayout(0),
			newSpacer(rect{0, 0, 620, 30}),
			nameText,
			newCheckBoxGroup(
				playHere,
				playAI,
				playNetwork,
			),
			ipText,
			portText,
			connectButton,
			newSpacer(rect{0, 0, 0, 30}),
		)
	}
	var playerTabs [4]*tab
	for i := range playerTabs {
		col := game.Color(i)
		playerTabs[i] = newTab(fullPlayerColor(col), playerMenus[i], i < settings.Settings.PlayerCount)
	}
	playersSheet := newTabSheet(60, playerTabs[:]...)

	newGameMenu := newWindow(
		rect{0, 0, gameW, gameH},
		newVerticalFlowLayout(20),
		newCheckBoxGroup(threePlayers, fourPlayers),
		playersSheet,
		newButton(lang.StartGame, rect{0, 0, 400, 80}, StartGameOption),
		newButton(lang.Back, rect{0, 0, 400, 80}, NewGameBackOption),
	)
	newGameMenu.setVisible(false)

	ui := &gameUI{
		game:           g,
		window:         win,
		camera:         newCamera(),
		graphics:       graphics,
		mainMenu:       mainMenu,
		newGameMenu:    newGameMenu,
		languageMenu:   languageMenu,
		playerTabSheet: playersSheet,
		lastPlayerTab:  playerTabs[3],
	}
	ui.buyMenu = newBuyMenu(graphics, ui)
	ui.gui = newComposite(ui.mainMenu, ui.newGameMenu, ui.languageMenu)
	if err := ui.init(); err != nil {
		return nil, err
	}
	ui.setLanguage(lang.Language(settings.Settings.Language))
	//ui.DEBUG_initGame()
	return ui, nil
}
Exemple #2
0
func (ui *gameUI) MouseButtonDown(button glfw.MouseButton) {
	if button != glfw.MouseButtonLeft {
		return // TODO handle right click when building to undo both buying and build
	}

	gameX, gameY := ui.camera.windowToGame(ui.mouseX, ui.mouseY)

	if ui.game.State == game.NotStarted {
		if action := ui.gui.click(gameX, gameY); action != -1 {
			switch action {
			case NewGameOption:
				ui.mainMenu.visible = false
				ui.newGameMenu.visible = true
			case ChooseLanguageOption:
				ui.mainMenu.visible = false
				ui.languageMenu.visible = true
			case QuitOption:
				ui.window.Close()
			case LanguageOKOption:
				ui.languageMenu.visible = false
				ui.mainMenu.visible = true
			case NewGameBackOption:
				ui.newGameMenu.visible = false
				ui.mainMenu.visible = true
			case StartGameOption:
				ui.game = game.New([]game.Color{game.Red, game.Blue, game.White}, rand.Int())
				ui.game = game.New([]game.Color{game.Red, game.Blue, game.White}, 0) // TODO remove
				ui.init()
				ui.game.Start()
			case ThreePlayersOption:
				ui.lastPlayerTab.visible = false
				ui.playerTabSheet.relayout()
				settings.Settings.PlayerCount = 3
			case FourPlayersOption:
				ui.lastPlayerTab.visible = true
				ui.playerTabSheet.relayout()
				settings.Settings.PlayerCount = 4
			}
			if action >= LanguageOptionOffset {
				language := lang.Language(action - LanguageOptionOffset)
				ui.setLanguage(language)
			}
		}
	} else if ui.game.State == game.ChoosingNextAction {
		ui.buyMenu.click(gameX, gameY)
	} else if ui.game.State == game.BuildingFirstSettlement ||
		ui.game.State == game.BuildingSecondSettlement ||
		ui.game.State == game.BuildingNewSettlement {
		corner, hit := screenToCorner(gameX, gameY)
		if hit && ui.game.CanBuildSettlementAt(corner) {
			ui.game.BuildSettlement(corner)
		}
	} else if ui.game.State == game.BuildingFirstRoad ||
		ui.game.State == game.BuildingSecondRoad ||
		ui.game.State == game.BuildingNewRoad {
		edge, hit := screenToEdge(gameX, gameY)
		if hit && ui.game.CanBuildRoadAt(edge) {
			ui.game.BuildRoad(edge)
		}
	} else if ui.game.State == game.BuildingNewCity {
		corner, hit := screenToCorner(gameX, gameY)
		if hit && ui.game.CanBuildCityAt(corner) {
			ui.game.BuildCity(corner)
		}
	} else if ui.game.State == game.RollingDice {
		center := rect{gameW/2 - 100, gameH/2 - 50, 200, 100}
		if center.contains(gameX, gameY) {
			ui.game.RollTheDice()
		}
	}
}