class PlayerController : ZilchComponent //This a player controller script for top down games or classic RPG styled games //NOTE: Must have the Sprite that this Script is attached to Dynamic in order for it to collide with //other objects in the world { [Property] var Speed : Real = 1.0; // How fast you move forward function Initialize(init : CogInitializer) { Zero.Connect(this.Space, Events.LogicUpdate, this.OnLogicUpdate); var startPosition = this.Space.LevelSettings.LevelVars.PlayerPosition; this.Owner.Transform.Translation = startPosition; } function OnLogicUpdate(updateEvent : UpdateEvent) { // The direction we want to move in var movement = Real3(0.0, 0.0, 0.0); // 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.Y += 1.0; } // If the Down Arrow key is held down if(Zero.Keyboard.KeyIsDown(Keys.S)) { // Set the movement direction to the opposite of the direction the player is facing movement.Y -= 1.0; } // If the Down Arrow key is held down if(Zero.Keyboard.KeyIsDown(Keys.A)) { // Set the movement direction to the opposite of the direction the player is facing movement.X -= 1.0; } if(Zero.Keyboard.KeyIsDown(Keys.D)) { // Set the movement direction to the opposite of the direction the player is facing movement.X += 1.0; } // Apply the recorded movement using the new rotation this.Owner.Transform.Translation += movement * this.Speed * updateEvent.Dt; } }