コード例 #1
0
        protected override void Start()
        {
            collider = Entity.GetComponent <Collider.Collider>();
            renderer = Entity.GetComponent <SpriteRenderer>();

            collider.IsTrigger  = true;
            collider.OnTrigger += (collidedEntity) =>
            {
                if (!collidedEntity.Name.StartsWith("floor"))
                {
                    return;
                }

                var fixedPosition = new Vector2(Entity.Transform.X, Entity.Transform.Y);

                var collidedEntityRenderer = collidedEntity.GetComponent <SpriteRenderer>();
                var collidedOnBottom = previousPosition.Y <Entity.Transform.Y && Entity.Transform.Y + renderer?.Height> collidedEntity.Transform.Y;
                var collidedOnTop = previousPosition.Y > Entity.Transform.Y && Entity.Transform.Y < collidedEntity.Transform.Y + collidedEntityRenderer?.Height;

                if (collidedOnBottom)
                {
                    fixedPosition.Y = collidedEntity.Transform.Y - (renderer?.Height ?? 1) - 1;
                }
                else if (collidedOnTop)
                {
                    fixedPosition.Y = collidedEntity.Transform.Y;
                }

                applyGravity = collidedOnBottom || collidedOnTop;

                Entity.Transform.FixPosition(fixedPosition);

                DisableGravity();
            };
        }
コード例 #2
0
 /// <summary>
 ///     When making a new GameObject, a new SpriteRenderer and Collider wil be made.
 /// </summary>
 /// <param name="radius">radius gameobject</param>
 /// <param name="image">image gameobject</param>
 /// <param name="canvas">canvas gameobject is drawn on</param>
 /// <param name="location">location of the gameobject</param>
 protected GameObject(int radius, Image image, Canvas canvas, Vector location)
 {
     Location = location;
     if (image != null || canvas != null)
     {
         Renderer = new SpriteRenderer(image, this, canvas);
     }
     Collider = new Collider.Collider(radius, this);
 }