Beispiel #1
0
        private void Update_(float dt)
        {
            if (pauseCount > 0)
            {
                return;
            }
            if (IsFinish())
            {
                return;
            }

            elapsed += dt;

            if (da.ignoreObstacles || da.collision)
            {
                dashRequest.Update(dt);
                Vector3 displacement = ((FixedUpdateDashRequest)dashRequest).Displacement_(movementComponent, dt);
                if (da.ignoreObstacles)
                {
                    movementComponent.ForceSetPosition(movementComponent.PositionV3 + displacement);
                }
                else
                {
                    movementComponent.Move(displacement);
                }
            }

            if (da.collision)
            {
                Vector2 centerOfHitbox   = GetCenterOfHitbox();
                Vector2 bridgeHitBoxSize = new Vector2(
                    Mathf.Abs(centerOfHitbox.x - centerOfHitBoxAtPreviousFrame.x),
                    casterBoxCollider.size.y
                    );
                Vector2 centerOfBridgeHitBox = (centerOfHitbox + centerOfHitBoxAtPreviousFrame) / 2;

                /*CubeShape cc = new CubeShape(centerOfBridgeHitBox, bridgeHitBoxSize, new List<SsarTuple<Color, float>>(new []{new SsarTuple<Color, float>(Color.yellow, .1f), }));
                 * GizmosDrawer.Instance.AddRequest(new DrawRequest(cc, .1f));*/
                cubeShape.SetPos(centerOfHitbox);
                cubeShape.size = casterBoxCollider.size;

                bool       collision         = false;
                Transform  collidedTransform = null;
                Collider   targetCollider    = null;
                Collider[] c = Physics.OverlapBox(centerOfBridgeHitBox, bridgeHitBoxSize / 2, Quaternion.identity, obstacleMask);
                foreach (Collider collider in c)
                {
                    //DLog.Log("Collide with obstacle " + collider.name);
                    if (collider.gameObject == mapGround)
                    {
                        continue;
                    }
                    if (collider.gameObject == mapCeil)
                    {
                        continue;
                    }

                    collision         = true;
                    collidedTransform = collider.transform;
                    targetCollider    = collider;
                    break;
                }
                if (!collision)
                {
                    c = Physics.OverlapBox(centerOfBridgeHitBox, bridgeHitBoxSize / 2, Quaternion.identity, creatureMask);
                    foreach (Collider collider in c)
                    {
                        //DLog.Log("Collide with creature " + collider.GetComponentInParent<EntityReference>().gameObject.name);
                        EntityReference collidedEntityRef = collider.GetComponentInParent <EntityReference>();
                        if (collidedEntityRef.transform == casterTransform)
                        {
                            continue;
                        }
                        if (collidedEntityRef.Entity.Group.Equals(casterEntity.Group))
                        {
                            continue;
                        }
                        collision = true;
                        if (collidedTransform == null)
                        {
                            collidedTransform = collider.transform;
                            targetCollider    = collider;
                        }
                        else
                        {
                            float distanceBetweenCasterAndTransform         = Vector2.Distance(positionAtPreviousFrame, collider.transform.position);
                            float distanceBetweenCasterAndPreviousTransform = Vector2.Distance(positionAtPreviousFrame, collidedTransform.position);
                            if (distanceBetweenCasterAndTransform < distanceBetweenCasterAndPreviousTransform)
                            {
                                collidedTransform = collider.transform;
                                targetCollider    = collider;
                            }
                        }
                    }
                }

                if (collision && !dispatched)
                {
                    Vector2 pos;
                    if (elapsed > dt)                      //second loop or above
                    //DLog.Log("Second and above loop");
                    {
                        Vector2 offset = da.offset.FlipFollowDirection(movementComponent.FacingDirection);
                        pos   = (Vector2)targetCollider.bounds.center - offset;
                        pos.y = movementComponent.Position.y;
                        float minX = centerOfHitBoxAtPreviousFrame.x - offset.x;
                        float maxX = centerOfHitbox.x - offset.x;
                        if (movementComponent.FacingDirection == Direction.Left)
                        {
                            minX = centerOfHitbox.x - offset.x;
                            maxX = centerOfHitBoxAtPreviousFrame.x - offset.x;
                        }

                        /*if (pos.x < minX || pos.x > maxX) {
                         *      DLog.Log("x " + pos.x + " minX " + minX + " maxX " + maxX);
                         * }*/
                        pos.x = Mathf.Clamp(pos.x, minX, maxX);
                    }
                    else                      //first loop
                                              //DLog.Log("First loop");
                    {
                        bool       alreadyCollided   = false;
                        Collider[] collidedCreatures = Physics.OverlapBox(centerOfHitBoxAtPreviousFrame, casterBoxCollider.size / 2, Quaternion.identity, creatureMask);
                        if (collidedCreatures.Contains(targetCollider))
                        {
                            alreadyCollided = true;
                        }

                        if (alreadyCollided)
                        {
                            pos = positionAtPreviousFrame;
                        }
                        else
                        {
                            Vector2 offset = da.offset.FlipFollowDirection(movementComponent.FacingDirection);
                            pos = (Vector2)targetCollider.bounds.center - offset;
                            if (movementComponent.FacingDirection == Direction.Right)
                            {
                                pos -= new Vector2(targetCollider.bounds.size.x / 2, 0);
                            }
                            else
                            {
                                pos += new Vector2(targetCollider.bounds.size.x / 2, 0);
                            }
                            pos.y = movementComponent.Position.y;
                        }
                    }

                    movementComponent.ForceSetPosition(pos);
                    cubeShape.SetPos(GetCenterOfHitbox());
                    dispatched = false;
                    skill.TriggerEventWithId(da.eventId);
                    dashRequest.Abort();
                }

                centerOfHitBoxAtPreviousFrame = centerOfHitbox;
            }

            positionAtPreviousFrame = movementComponent.Position;
            if (IsFinish())
            {
                ResetColliderToOriginalValues();
            }
        }