Пример #1
0
func main() {
	log.Println("Welcome to The Game!")

	config, err := configs.NewConfig("config/settings.json")
	if err != nil {
		log.Panicf("Unable to read settings.json: %v", err)
	}

	game := NewGame(config)
	game.Run()
	defer game.Shutdown()
}
func Test_InputDispatcher_BuildsKeybindingsFromConfig(t *testing.T) {
	config, _ := configs.NewConfig("testdata/keybindings.json")
	emitter := NewTestEmitter()
	keyboard := NewKeyboard(emitter)
	mouse := NewMouse(emitter)
	dispatcher := NewInputDispatcher(config, keyboard, mouse)

	quitFired := 0
	dispatcher.On(events.Quit, func(events.Event) {
		quitFired += 1
	})

	zoomFired := 0
	dispatcher.On(events.ZoomIn, func(events.Event) {
		zoomFired += 1
	})

	attackFired := 0
	dispatcher.On(events.Primary, func(events.Event) {
		attackFired += 1
	})

	// See the test file keybindings.json
	// 3 Quit keys
	emitter.fireKeyCallback(KeyQ, KeyPressed)
	emitter.fireKeyCallback(KeyEsc, KeyPressed)
	emitter.fireKeyCallback(KeyX, KeyPressed)

	// One ZoomIn key
	emitter.fireKeyCallback(KeyZ, KeyPressed)

	// A mouse button
	emitter.fireMouseButtonCallback(Mouse1, KeyPressed)

	assert.Equal(t, 3, quitFired)
	assert.Equal(t, 1, zoomFired)
	assert.Equal(t, 1, attackFired)
}
func GetInputDispatcher() (*InputDispatcher, *Keyboard, *TestEmitter) {
	config, _ := configs.NewConfig("testdata/blank.json")
	emitter := NewTestEmitter()
	keyboard := NewKeyboard(emitter)
	return NewInputDispatcher(config, keyboard), keyboard, emitter
}