Example #1
0
File: rl.go Project: skyview059/vu
// Update is the regular engine callback.
func (rl *rltag) Update(eng vu.Eng, in *vu.Input, s *vu.State) {
	run := 5.0    // move so many cubes worth in one second.
	spin := 270.0 // spin so many degrees in one second.
	if in.Resized {
		rl.resize(s.W, s.H)
	}

	// pre-process user presses.
	// reduce individual move amounts for multiple move requests.
	dt := in.Dt
	moveDelta := dt * 2
	for press, _ := range in.Down {
		switch press {
		case vu.K_W, vu.K_S, vu.K_Q, vu.K_E:
			moveDelta *= 0.5
		}
	}

	// process user presses.
	for press, down := range in.Down {
		switch press {
		case vu.K_W:
			rl.flr.cam.Move(0, 0, moveDelta*-run, rl.flr.cam.Lookxz())
			rl.arrow.SetLocation(rl.flr.cam.Location())
		case vu.K_S:
			rl.flr.cam.Move(0, 0, moveDelta*run, rl.flr.cam.Lookxz())
			rl.arrow.SetLocation(rl.flr.cam.Location())
		case vu.K_Q:
			rl.flr.cam.Move(moveDelta*-run, 0, 0, rl.flr.cam.Lookxz())
			rl.arrow.SetLocation(rl.flr.cam.Location())
		case vu.K_E:
			rl.flr.cam.Move(moveDelta*run, 0, 0, rl.flr.cam.Lookxz())
			rl.arrow.SetLocation(rl.flr.cam.Location())
		case vu.K_A:
			rl.flr.cam.AdjustYaw(dt * spin)
			rl.arrow.SetRotation(rl.flr.cam.Lookxz())
		case vu.K_D:
			rl.flr.cam.AdjustYaw(dt * -spin)
			rl.arrow.SetRotation(rl.flr.cam.Lookxz())
		case vu.K_1, vu.K_2, vu.K_3, vu.K_4, vu.K_5, vu.K_6, vu.K_7, vu.K_8, vu.K_9, vu.K_0:
			if down == 1 {
				rl.setLevel(eng, press)
			}
		}
	}

	// show some stats to see the effectiveness of culling.
	allModels, allVerts := eng.Modelled()
	renModels, renVerts := eng.Rendered()
	modelStats := fmt.Sprintf("%d  models    culled to %d", allModels, renModels)
	vertexStats := fmt.Sprintf("%d verticies culled to %d", allVerts, renVerts)
	rl.flr.modelStats.SetPhrase(modelStats)
	rl.flr.vertexStats.SetPhrase(vertexStats)

	// http://stackoverflow.com/questions/87304/calculating-frames-per-second-in-a-game
	t := eng.Usage()
	rl.elapsed += t.Elapsed
	rl.update += t.Update
	rl.renders += t.Renders
	if in.Ut%50 == 0 { // average over 50 updates.
		fps := float64(rl.renders) / rl.elapsed.Seconds()
		update := rl.update.Seconds() / 50.0 * 1000
		timings := fmt.Sprintf("FPS %2.2f Update %3.2fms", fps, update)
		rl.flr.times.SetPhrase(timings)
		rl.renders = 0
		rl.elapsed = 0
		rl.update = 0
	}
}