Exemple #1
0
// CreateGoalEntity creates the game's goal entity and places
// it at a good spot in the world.
func (s *Server) createGoalEntity() {
	// calculate a center location
	centerX := math.Floor(float64((float32(s.levelWidth)/2.0)*float32(landscape.ChunkSize))) + 0.5
	centerZ := math.Floor(float64((float32(s.levelHeight)/2.0)*float32(landscape.ChunkSize))) + 0.5
	tallest, err := s.landscapeMan.GetHeightAt(int(centerX), int(centerZ))
	if err != nil {
		groggy.Logsf("ERROR", "S: CreateGoalLocation() couldn't figure out the height at X,Z: %f,%f. %v", centerX, centerZ, err)
		return
	}

	// create the entity
	groggy.Logsf("DEBUG", "S: CreateGoalLocation() will create goal at %f,%f,%f for height of %d.", centerX, float32(tallest)+1.5, centerZ, tallest)
	playerGoal := s.entityManager.RegisterNewEntity()
	playerGoal.Name = "PlayerGoal"
	playerGoal.AssetPath = "goal"
	playerGoal.Location = mgl.Vec3{float32(centerX), float32(tallest) + 1.5, float32(centerZ)}
	playerGoal.Collider = physics.NewCollisionCube(nil, physmath.Vector3{0.5, 0.5, 0.5})
	goalBody := playerGoal.Collider.GetBody()
	goalBody.SetInfiniteMass()
	goalBody.Acceleration = physmath.Vector3{0.0, 0.0, 0.0}
	playerGoal.SyncToColliders()
	goalBody.CalculateDerivedData()
	playerGoal.Collider.CalculateDerivedData()

	// set the goal for the player
	s.player.Goal = playerGoal
}
Exemple #2
0
func fire() {
	const cubesToMake = 4
	var offset float32 = 0.0
	if len(cubes) > 0 && (len(cubes)/cubesToMake)%2 >= 1 {
		offset = 0.75
	}

	for i := 0; i < cubesToMake; i++ {
		e := new(Entity)
		e.Node = CreateCube(-0.5, -0.5, -0.5, 0.5, 0.5, 0.5)
		e.Node.Shader = diffuseShader
		e.Node.Color = mgl.Vec4{1.0, 1.0, 1.0, 1.0}
		e.Node.Location = mgl.Vec3{float32(i*2.0-cubesToMake/2) - 0.5 + offset, 10.0, 0.0}
		e.Node.Tex0 = crateTexture

		// create the collision box for the the cube
		cubeCollider := cubez.NewCollisionCube(nil, m.Vector3{0.5, 0.5, 0.5})
		cubeCollider.Body.Position = m.Vector3{m.Real(i*2.0-cubesToMake/2) - 0.5 + m.Real(offset), 10.0, 0.0}
		cubeCollider.Body.SetMass(8.0)
		cubeCollider.Body.CanSleep = true

		var cubeInertia m.Matrix3
		cubeInertia.SetBlockInertiaTensor(&cubeCollider.HalfSize, 8.0)
		cubeCollider.Body.SetInertiaTensor(&cubeInertia)

		cubeCollider.Body.CalculateDerivedData()
		cubeCollider.CalculateDerivedData()
		e.Collider = cubeCollider

		cubes = append(cubes, e)
	}
}
Exemple #3
0
func buildCollider(cb *collisionBlock) physics.Collider {
	length := physmath.Real(cb.EndZ - cb.StartZ + 1)
	halfLength := physmath.Real(length) / 2.0

	// create the collision box for the the cube
	cubeCollider := physics.NewCollisionCube(nil, physmath.Vector3{0.5, 0.5, halfLength})
	cubeCollider.Body.Position = physmath.Vector3{physmath.Real(cb.X) + 0.5, physmath.Real(cb.Y) + 0.5, physmath.Real(cb.StartZ) + halfLength}
	cubeCollider.Body.SetInfiniteMass()
	cubeCollider.Body.CanSleep = false
	cubeCollider.Body.CalculateDerivedData()
	cubeCollider.CalculateDerivedData()

	return cubeCollider
}
Exemple #4
0
// ConnectPlayer makes a new entity for the connecting player
// and gives it the name specified.
func (s *Server) connectPlayer(name string) *Player {
	// create the player entity
	playerEntity := s.entityManager.RegisterNewEntity()
	playerEntity.Name = name
	playerEntity.AssetPath = "player"
	playerEntity.IsPlayerControlled = true
	playerEntity.Collider = physics.NewCollisionCube(nil, physmath.Vector3{0.45, 0.75, 0.45})
	playerBody := playerEntity.Collider.GetBody()
	playerBody.SetMass(100.0)
	playerBody.CanSleep = false

	// NOTE: by not setting up any inertia tensor, we will effectively make this cube
	// not rotate on impact.

	// create the player object
	p := NewPlayer(playerEntity)

	// for now, the server only deals with one player
	s.player = p

	// return the new player
	return p
}
Exemple #5
0
func main() {
	app = NewApp()
	app.InitGraphics("Ballistic", 800, 600)
	app.SetKeyCallback(keyCallback)
	app.OnRender = renderCallback
	app.OnUpdate = updateCallback
	defer app.Terminate()

	// compile the shaders
	var err error
	colorShader, err = LoadShaderProgram(DiffuseColorVertShader, DiffuseColorFragShader)
	if err != nil {
		panic("Failed to compile the shader! " + err.Error())
	}

	// create the ground plane
	groundPlane = cubez.NewCollisionPlane(m.Vector3{0.0, 1.0, 0.0}, 0.0)

	// make a ground plane to draw
	ground = CreatePlaneXZ(-500.0, 500.0, 500.0, -500.0, 1.0)
	ground.Shader = colorShader
	ground.Color = mgl.Vec4{0.6, 0.6, 0.6, 1.0}

	// create a test cube to render
	cubeNode := CreateCube(-1.0, -1.0, -1.0, 1.0, 1.0, 1.0)
	cubeNode.Shader = colorShader
	cubeNode.Color = mgl.Vec4{1.0, 0.0, 0.0, 1.0}

	// create the collision box for the the cube
	var cubeMass m.Real = 8.0
	var cubeInertia m.Matrix3
	cubeCollider := cubez.NewCollisionCube(nil, m.Vector3{1.0, 1.0, 1.0})
	cubeCollider.Body.Position = m.Vector3{0.0, 5.0, 0.0}
	cubeCollider.Body.SetMass(cubeMass)
	cubeInertia.SetBlockInertiaTensor(&cubeCollider.HalfSize, cubeMass)
	cubeCollider.Body.SetInertiaTensor(&cubeInertia)
	cubeCollider.Body.CalculateDerivedData()
	cubeCollider.CalculateDerivedData()

	// make the entity out of the renerable and collider
	cube = NewEntity(cubeNode, cubeCollider)

	// make a slice of entities for bullets
	bullets = make([]*Entity, 0, 16)

	// make the backboard to bound the bullets off of
	backboardNode := CreateCube(-0.5, -2.0, -0.25, 0.5, 2.0, 0.25)
	backboardNode.Shader = colorShader
	backboardNode.Color = mgl.Vec4{0.25, 0.2, 0.2, 1.0}
	backboardCollider := cubez.NewCollisionCube(nil, m.Vector3{0.5, 2.0, 0.25})
	backboardCollider.Body.Position = m.Vector3{0.0, 2.0, -10.0}
	backboardCollider.Body.SetInfiniteMass()
	backboardCollider.Body.CalculateDerivedData()
	backboardCollider.CalculateDerivedData()
	SetGlVector3(&backboardNode.Location, &backboardCollider.Body.Position)

	// make the backboard entity
	backboard = NewEntity(backboardNode, backboardCollider)

	// setup the camera
	app.CameraPos = mgl.Vec3{-3.0, 3.0, 15.0}
	app.CameraRotation = mgl.QuatLookAtV(
		mgl.Vec3{-3.0, 3.0, 15.0},
		mgl.Vec3{0.0, 1.0, 0.0},
		mgl.Vec3{0.0, 1.0, 0.0})

	gl.Enable(gl.DEPTH_TEST)
	app.RenderLoop()
}