class CP1PlayerController : ZilchComponent { // How fast you move forward [Property] var Speed : Real = 1.0; // How fast you rotate [Property] var RotationSpeed : Real = 1.0; // How many lives the player has [Property] var Lives : Integer = 3; var SpawnPosition : Real3 = Real3(0.0,0.0,0.0); function Initialize(init : CogInitializer) { // Record the initial position of the player as the spawn position this.SpawnPosition = this.Owner.Transform.Translation; // Connect to logic update for movement and rotation Zero.Connect(this.Space, Events.LogicUpdate, this.OnLogicUpdate); } function OnLogicUpdate(updateEvent : UpdateEvent) { // The angle we are going to rotate by var rotationAngles = local Real3(0.0, 0.0, 0.0); // If the Left Arrow key is held down if (Zero.Keyboard.KeyIsDown(Keys.A)) { rotationAngles.Z += this.RotationSpeed; } // If the Right Arrow key is held down if (Zero.Keyboard.KeyIsDown(Keys.D)) { rotationAngles.Z -= this.RotationSpeed; } // Apply the calculated rotation first so movement is not base of the rotation from the previous frame this.Owner.Transform.RotateAnglesWorld(rotationAngles * updateEvent.Dt); // The direction we want to move in var movement = local Real3(0.0, 0.0, 0.0); // Get our facing direction from the Orientation component var forwardDirection = this.Owner.Orientation.WorldForward; // If the Up Arrow key is held down if (Zero.Keyboard.KeyIsDown(Keys.W)) { // Set the movement direction to the direction the player is facing movement += forwardDirection; } // If the Down Arrow key is held down else if(Zero.Keyboard.KeyIsDown(Keys.S)) { // Set the movement direction to the opposite of the direction the player is facing movement -= forwardDirection; } // Make sure the we only get the direction of the vector: movement = Math.Normalize(movement); // Calculate the force that should be applied to accelerate the object var force = movement * this.Speed; // apply the movement force to the player this.Owner.RigidBody.ApplyForce(force); } function Kill() { // Remove a life from the player this.Lives -= 1; // If the player has lives left after dying if(this.Lives > 0) { // Reset the player's translation to the starting spawn position this.Owner.Transform.Translation = this.SpawnPosition; // Reset the playe velocity for spawn this.Owner.RigidBody.Velocity = local Real3(0.0,0.0,0.0); // Print the remaining live the player has left Console.WriteLine("Player has `this.Lives` left after dying."); } // If the player is out of lives else { //Print a loss message to the player Console.WriteLine("Player has no more lives after dying."); Console.WriteLine("Game Over"); //Destroy the player this.Owner.Destroy(); } } }