Beispiel #1
0
        public static GameObject InitHitBoxComponent(EntityKey entityKey, IHitBox playerEntity, GameObject hitboxGo)
        {
            hitboxGo.name = "hitbox_" + entityKey;
            GameObject bsGo = HitBoxConstants.FindBoundingSphereModel(hitboxGo);

            SphereCollider sc = bsGo.GetComponent <SphereCollider>();

            sc.enabled = false;

            hitboxGo.transform.Recursively(t =>
            {
                var go = t.gameObject;
                HitBoxOwnerComponent pc = go.GetComponent <HitBoxOwnerComponent>();
                if (pc == null)
                {
                    pc = go.AddComponent <HitBoxOwnerComponent>();
                }
                pc.OwnerEntityKey   = entityKey;
                pc.gameObject.layer = UnityLayers.HitBoxLayer;
            });
            playerEntity.AddHitBox(new BoundingSphere(sc.center, sc.radius), hitboxGo);
            hitboxGo.SetActive(false);

            return(hitboxGo);
        }
Beispiel #2
0
 public bool Intersects(IHitBox hitBox)
 {
     if (hitBox is QuadrilateralHitbox)
     {
         var            quad    = (QuadrilateralHitbox)hitBox;
         List <Vector2> projVec = new List <Vector2>();
         int            n       = _points.Length;
         for (int i = 0; i < n; i++)
         {
             var vec = _points[(i + 1) % n] - _points[i];
             vec.Normalize();
             projVec.Add(new Vector2(-vec.Y, vec.X));
         }
         for (int i = 0; i < n; i++)
         {
             var vec = quad._points[(i + 1) % n] - quad._points[i];
             vec.Normalize();
             projVec.Add(new Vector2(-vec.Y, vec.X));
         }
         foreach (var proj in projVec)
         {
             var A = getProjections(proj);
             var B = quad.getProjections(proj);
             if (Math.Max(A.X, B.X) > Math.Min(A.Y, B.Y))
             {
                 return(false);
             }
         }
         return(true);
     }
     return(false);
 }
Beispiel #3
0
 private void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.layer == Constants.HITBOX_LAYER)
     {
         IHitBox hitBox = other.GetComponent <IHitBox>();
         hitBox.takeDamage(0f); //TODO  decide how much damage it would do
     }
 }
    private void Start()
    {
        hitBox = hitTarget.GetComponent <IHitBox>();
        if (gameObject == hitTarget)
        {
            return;
        }

        Destroy(this);
        Debug.LogError($"Wrong place for hit box on EnemyGraphics{gameObject.name}");
    }
Beispiel #5
0
        private IHitBox GetTarget()
        {
            IHitBox      target = null;
            RaycastHit2D hit    = Physics2D.Raycast(shootPoint.position, shootPoint.right, weaponData.range);

            if (hit.collider != null)
            {
                target = hit.transform.root.GetComponent <IHitBox>();
            }
            return(target);
        }
Beispiel #6
0
    private IHitBox GetTarget()
    {
        IHitBox      target = null;
        RaycastHit2D hit    = Physics2D.Raycast(attackPoint.position, attackPoint.right, weaponData.WeaponRange);

        if (hit.collider != null)
        {
            target = hit.transform.gameObject.GetComponent <IHitBox>();
        }

        return(target);
    }
        public GameObject()
        {
            ObjectCenter = new Vector3();
            Speed = new Vector3();
            Acceleration = new Vector3();

            Rotation = new Vector3();
            RotationSpeed = new Vector3();
            RotationAceleration = new Vector3();

            Mass = 0;
            Hp = 0;

            MainHitBox = null;
            masterObject = null;
            slaveObjects = new List<GameObject>();
        }
Beispiel #8
0
        private bool CanSetSurfaced(IHitBox hitBox)
        {
            FloatRectangle bounds = hitBox.BoundingBox.Bounds;
            float x = bounds.X;
            float w = bounds.Width;
            float y = bounds.Bottom;
            float h = MagicNumbers.AcceptableSurfaceDistance;

            var rectangle = new FloatRectangle(x, y, w, h);

            var fit = CollisionDetection.CanFitInMatrix(rectangle);
            if (fit == FitResult.Solid) // ensure that the surface actually blocks further movement.
            {
                return true;
            }

            return false;
        }
Beispiel #9
0
        public MoveResult Move(IHitBox hitBox, Vector2 interpolation)
        {
            AxisAlignedBoundingBox aabb = hitBox.BoundingBox;

            var result = CollisionDetection.CanMove(aabb.Bounds, interpolation, DetectionType.Collision);
            if (result.HasFlag(MoveResult.LevelCompleted))
            {
                result = MoveResult.LevelCompleted;
            }
            else
            {
                result = Reposition(hitBox, result, interpolation);
            }

            Diagnostic.Write("aabb", aabb.Position);

            return result;
        }
Beispiel #10
0
        public static void InitHitBoxComponent(EntityKey entityKey, IHitBox playerEntity,
                                               HitBoxTransformProvider provider)
        {
            provider.SetActive(false);
            playerEntity.ReplaceHitBox(-1, new BoundingSphere(provider.BoundSpherePosition(), provider.BoundSphereRadius()),
                                       null, false);
            var trans = provider.GetHitBoxTransforms();

            foreach (var item in trans)
            {
                var comp = item.Value.gameObject.GetComponent <HitBoxOwnerComponent>();
                if (comp == null)
                {
                    comp = item.Value.gameObject.AddComponent <HitBoxOwnerComponent>();
                }
                comp.OwnerEntityKey = entityKey;
            }
            provider.FlushLayerOfHitBox();
        }
Beispiel #11
0
        public MoveResult Move(IHitBox hitBox, Vector2 interpolation)
        {
            AxisAlignedBoundingBox aabb = hitBox.BoundingBox;

            var result = CollisionDetection.CanMove(aabb.Bounds, interpolation, DetectionType.Collision);

            if (result.HasFlag(MoveResult.LevelCompleted))
            {
                result = MoveResult.LevelCompleted;
            }
            else
            {
                result = Reposition(hitBox, result, interpolation);
            }

            Diagnostic.Write("aabb", aabb.Position);

            return(result);
        }
Beispiel #12
0
        private bool CanSetSurfaced(IHitBox hitBox)
        {
            FloatRectangle bounds = hitBox.BoundingBox.Bounds;
            float          x      = bounds.X;
            float          w      = bounds.Width;
            float          y      = bounds.Bottom;
            float          h      = MagicNumbers.AcceptableSurfaceDistance;

            var rectangle = new FloatRectangle(x, y, w, h);

            var fit = CollisionDetection.CanFitInMatrix(rectangle);

            if (fit == FitResult.Solid)             // ensure that the surface actually blocks further movement.
            {
                return(true);
            }

            return(false);
        }
        public bool IsHit(IHitBox hitingObject)
        {
            if(hitingObject.GetType() == typeof(SimpleHitBox))
            {
                var hitBox = (SimpleHitBox)hitingObject;
                var gameObject = hitBox.LinkedObject;
               
                double distane = (gameObject.GetAbsoluteObjectCenter() - LinkedObject.GetAbsoluteObjectCenter()).Length;
                double distanceToHit = hitBox.Radius + Radius;
                if (distanceToHit < distane) return false;
                return true;

                //double distance = 
            }
            else
            {
                throw new NotImplementedException();
            }
            
        }
Beispiel #14
0
        private MoveResult Reposition(IHitBox hitBox, MoveResult result, Vector2 interpolation)
        {
            AxisAlignedBoundingBox aabb = hitBox.BoundingBox;

            var moveX = result.HasFlag(MoveResult.X) &&
                        !result.HasFlag(MoveResult.BlockedOnNegativeX) &&
                        !result.HasFlag(MoveResult.BlockedOnPositiveX);

            if (moveX)
            {
                aabb.Position.X += interpolation.X;
            }

            bool surfaced = CanSetSurfaced(hitBox);
            var  moveY    = result.HasFlag(MoveResult.Y) &&
                            !result.HasFlag(MoveResult.BlockedOnNegativeY) &&
                            !result.HasFlag(MoveResult.BlockedOnPositiveY);

            if ((moveY || !surfaced) && !result.HasFlag(MoveResult.Died))
            {
                hitBox.State = HitBoxState.Airborne;
            }
            else
            {
                hitBox.State = HitBoxState.Surfaced;
            }

            if (moveY && !result.HasFlag(MoveResult.Died))
            {
                aabb.Position.Y += interpolation.Y;
            }

            var reverse = CollisionDetection.CanMove(aabb.Bounds, -interpolation, DetectionType.Retrace);

            if (moveX)             // fix issue when moving on X axis to the left.
            {
                if (reverse.HasFlag(MoveResult.BlockedOnNegativeX) && interpolation.X > 0)
                {
                    aabb.Position.X -= interpolation.X;
                    result          &= ~MoveResult.X;
                }
                else if (reverse.HasFlag(MoveResult.BlockedOnPositiveX) && interpolation.X < 0)
                {
                    aabb.Position.X -= interpolation.X;
                    result          &= ~MoveResult.X;
                }
            }

            if (moveY)             // fix issue when hitting an impassable on Y axis when jumping.
            {
                if (reverse.HasFlag(MoveResult.BlockedOnNegativeY) && interpolation.Y > 0)
                {
                    aabb.Position.Y -= interpolation.Y;
                    result          &= ~MoveResult.Y;
                }
                else if (reverse.HasFlag(MoveResult.BlockedOnPositiveY) && interpolation.Y < 0)
                {
                    aabb.Position.Y -= interpolation.Y;
                    result          &= ~MoveResult.Y;
                }
            }

            return(result);
        }
Beispiel #15
0
 public bool Intersects(IHitBox hitBox)
 {
     return(false);
 }
Beispiel #16
0
        private MoveResult Reposition(IHitBox hitBox, MoveResult result, Vector2 interpolation)
        {
            AxisAlignedBoundingBox aabb = hitBox.BoundingBox;

            var moveX = result.HasFlag(MoveResult.X)
                && !result.HasFlag(MoveResult.BlockedOnNegativeX)
                && !result.HasFlag(MoveResult.BlockedOnPositiveX);

            if (moveX)
            {
                aabb.Position.X += interpolation.X;
            }

            bool surfaced = CanSetSurfaced(hitBox);
            var moveY = result.HasFlag(MoveResult.Y)
                && !result.HasFlag(MoveResult.BlockedOnNegativeY)
                && !result.HasFlag(MoveResult.BlockedOnPositiveY);

            if ((moveY || !surfaced) && !result.HasFlag(MoveResult.Died))
            {
                hitBox.State = HitBoxState.Airborne;
            }
            else
            {
                hitBox.State = HitBoxState.Surfaced;
            }

            if (moveY && !result.HasFlag(MoveResult.Died))
            {
                aabb.Position.Y += interpolation.Y;
            }

            var reverse = CollisionDetection.CanMove(aabb.Bounds, -interpolation, DetectionType.Retrace);
            if (moveX) // fix issue when moving on X axis to the left.
            {
                if (reverse.HasFlag(MoveResult.BlockedOnNegativeX) && interpolation.X > 0)
                {
                    aabb.Position.X -= interpolation.X;
                    result &= ~MoveResult.X;
                }
                else if (reverse.HasFlag(MoveResult.BlockedOnPositiveX) && interpolation.X < 0)
                {
                    aabb.Position.X -= interpolation.X;
                    result &= ~MoveResult.X;
                }
            }

            if (moveY) // fix issue when hitting an impassable on Y axis when jumping.
            {
                if (reverse.HasFlag(MoveResult.BlockedOnNegativeY) && interpolation.Y > 0)
                {
                    aabb.Position.Y -= interpolation.Y;
                    result &= ~MoveResult.Y;
                }
                else if (reverse.HasFlag(MoveResult.BlockedOnPositiveY) && interpolation.Y < 0)
                {
                    aabb.Position.Y -= interpolation.Y;
                    result &= ~MoveResult.Y;
                }
            }

            return result;
        }