///<summary> ///Define intial values for a given PCharacterInstance, such as name, sprite, collision, physics, etc... ///</summary> public static void InitializePCharacter(PCharacterInstance instance) { instance.Name = "Instance_" + instance.Base.Name; instance.CurrentAnimation = new PCharacterInstanceAnimation(); instance.CurrentAnimation.Clip = new PCharacterAnimation(); instance.Controller = new PCharacterInstanceController(); instance.Controller.Actions = new List <PControllerActionType>(); instance.RenderObject = new GameObject(instance.Base.Name); instance.Render = instance.RenderObject.AddComponent <SpriteRenderer>(); instance.RenderObject.transform.parent = instance.transform; instance.RenderObject.transform.localPosition = Vector2.zero; Refresh(instance); }
///<summary> ///Update PCharacterInstance physics and collision values ///</summary> public static void Refresh(PCharacterInstance instance) { if (instance.Base.CharacterType == PCharacterType.CHARACTER) { instance.Rig = instance.gameObject.GetComponent <Rigidbody2D>(); if (instance.Rig == null) { instance.Rig = instance.gameObject.AddComponent <Rigidbody2D>(); } instance.Rig.gravityScale = instance.Base.Configuration.GravityScale; instance.Rig.constraints = instance.Base.Configuration.Constraints2D; } if (Application.isPlaying && instance.Base.Animations.Length > 0 && instance.Base.Animations[0].PFrames.Length > 0) { instance.Render.sprite = instance.Base.CharacterAtlas.GetSprite(instance.Base.Animations[0].PFrames[0].FrameName); } RecalculateCollider((PCharacterInstance)instance); }
///<summary> ///Set collider2D values to boudaires of the current rendered sprite of the PCharacterInstance ///</summary> public static void RecalculateCollider(PCharacterInstance instance) { BoxCollider2D collider = instance.RenderObject.GetComponent <BoxCollider2D>(); if (collider == null) { collider = instance.RenderObject.AddComponent <BoxCollider2D>(); } if (instance.Render.sprite != null) { Sprite _sprite = instance.Render.sprite; Vector2 spriteBounds = instance.Render.sprite.bounds.size; collider.size = spriteBounds; collider.offset = new Vector2(0, collider.size.y / 2); return; } collider.offset = Vector2.zero; collider.size = Vector2.one; }