Esempio n. 1
0
    public void vehicleIrritationAction(Vehicle vehicle)
    {
        Vector3    vehiclePosition = vehicle.gameObject.transform.position;
        Quaternion vehicleRotation = vehicle.gameObject.transform.rotation;

        Vector3 vehicleToHuman  = transform.position - vehiclePosition;
        Vector3 vehicleMovement = vehicleRotation * Vector3.right;
        bool    isToTheRight    = Vector3.Cross(vehicleMovement, vehicleToHuman).z <= 0;

        Quaternion humanMovementRotation = Quaternion.Euler(0f, 0f, vehicleRotation.eulerAngles.z + (isToTheRight ? -90f : 90f));
        Vector3    offsetPosition        = humanMovementRotation * (Vector3.right * vehicle.GetComponent <VehicleInfo> ().vehicleWidth);

        deviationTarget      = transform.position + offsetPosition;
        timedDeviationTarget = new TimedDeviationTarget(4f, walkPath [0] + offsetPosition);
    }
Esempio n. 2
0
 private Vector3 getTargetPoint()
 {
     if (deviationTarget != INVALID_POINT)
     {
         return(deviationTarget);
     }
     else if (timedDeviationTarget != null)
     {
         timedDeviationTarget.time -= Time.deltaTime;
         Vector3 point = timedDeviationTarget.point;
         if (timedDeviationTarget.time <= 0f)
         {
             timedDeviationTarget = null;
         }
         return(point);
     }
     return(walkPath [0]);
 }
Esempio n. 3
0
    // Update is called once per frame
    void Update()
    {
        if (destroying)
        {
            return;
        }

        if (!paused && Game.isMovementEnabled())
        {
            if (waitTime > 0f)
            {
                waitTime -= Time.deltaTime;
                stats [STAT_WAITING_TIME].add(Time.deltaTime);
            }
            else if (waitForHumans.Any())
            {
                // Do nothing
                stats [STAT_WAITING_TIME].add(Time.deltaTime);
            }
            else
            {
                if (!vehiclesInVision.Any() || (deviationTarget != INVALID_POINT && vehiclesInVision.Any()))
                {
                    // Go to deviation target if set, otherwise according to walkPath
                    Vector3 targetPoint  = getTargetPoint();
                    Vector3 currentPoint = transform.position;
                    rotateHuman(targetPoint, currentPoint);

                    float travelDistance = Misc.getDistance(currentPoint, targetPoint);

                    float targetSpeedKmH = TARGET_WALKING_SPEED_KMH * speedFactor;
                    // TODO - Debug only - TURBO
                    targetSpeedKmH *= HumanLogic.TURBO;
                    float travelLengthThisFrame = (targetSpeedKmH * Time.deltaTime) / KPH_TO_LONGLAT_SPEED;

                    if (travelLengthThisFrame >= travelDistance)
                    {
                        transform.position = targetPoint;
                        if (deviationTarget != INVALID_POINT)
                        {
                            deviationTarget = INVALID_POINT;
                        }
                        else if (timedDeviationTarget != null)
                        {
                            timedDeviationTarget = null;
                        }
                        else
                        {
                            if (wayPointsLoop && wayPoints[0] == path[1])
                            {
                                // We have reached our first wayPoint and should loop, place first waypoint last and recalculate route
                                wayPoints.RemoveAt(0);
                                wayPoints.Add(path[1]);
                                bool lastPathIsTmpPath = path[path.Count - 1].Id == -1L;
                                Pos  possibleTmpEndPos = lastPathIsTmpPath ? path[path.Count - 1] : null;
                                Pos  endPos            = lastPathIsTmpPath ? path[path.Count - 2] : path[path.Count - 1];
                                path = Game.calculateCurrentPaths(path[1], endPos, null, wayPoints, false, true);
                                if (lastPathIsTmpPath)
                                {
                                    path.Add(possibleTmpEndPos);
                                }

                                walkPath = Misc.posToVector3(path);
                                // Walkpath is always containing upcoming positions
                                walkPath.RemoveAt(0);
                            }
                            else
                            {
                                walkPath.RemoveAt(0);
                                path.RemoveAt(0);

                                if (walkPath.Count == 0)
                                {
                                    fadeOutAndDestroy();
                                }
                            }
                        }
                    }
                    else
                    {
                        Vector3 movement = transform.rotation * (Vector3.right * travelLengthThisFrame);
                        transform.position = transform.position + movement;
                    }

                    float metersWalked = Mathf.Abs(Misc.kmhToMps(targetSpeedKmH) * Time.deltaTime);
                    totalWalkingDistance += metersWalked;

                    stats [STAT_WALKING_TIME].add(Time.deltaTime);
                    stats [STAT_WALKING_DISTANCE].add(metersWalked);
                }
            }
        }
    }
Esempio n. 4
0
    public void reportCollision(Collider col, string colliderName)
    {
        // ColliderName = "BODY" or "VISION"
        if (!destroying)
        {
            CollisionObj rawCollisionObj = getColliderType(col);
            if (rawCollisionObj != null)
            {
                VehicleCollisionObj vehicleCollisionObj = rawCollisionObj.typeName == VehicleCollisionObj.NAME ? (VehicleCollisionObj)rawCollisionObj : null;
                HumanCollisionObj   humanCollisionObj   = rawCollisionObj.typeName == HumanCollisionObj.NAME ? (HumanCollisionObj)rawCollisionObj : null;

                if (vehicleCollisionObj != null)
                {
                    setVehicleInVision(vehicleCollisionObj);
                }
                else if (humanCollisionObj != null)
                {
                    HumanLogic otherHuman   = humanCollisionObj.Human;
                    bool       shouldDecide = humanId < otherHuman.humanId;
                    if (colliderName == "VISION")
                    {
                        // Scenarios:
                        // Meeting in intersection, moving towards same point
                        // One walking faster, moving in same direction (towards same point)
                        // Meeting, not moving towards same point
                        // One or both have already deviated from target, meeting on the way to temporary point
                        float angle = Quaternion.Angle(transform.rotation, otherHuman.transform.rotation);
                        if (shouldDecide || angle < 20f)
                        {
                            if (angle >= 20f && angle <= 160f)
                            {
                                // Walking with an angle towards each other - one should wait
                                otherHuman.waitForAWhile();
                            }
                            else if (angle < 20f)
                            {
                                // Walking alongside, they divide their speed, the one ahead goes a tiny bit quicker
                                float totalWalkingSpeed = speedFactor + otherHuman.speedFactor;
                                speedFactor            = totalWalkingSpeed * 0.45f;
                                otherHuman.speedFactor = totalWalkingSpeed * 0.55f;
                            }
                            else
                            {
                                // Walking towards each other, decide one deviation point for each
                                Vector3    otherPosition = otherHuman.transform.position;
                                Quaternion otherRotation = otherHuman.transform.rotation;

                                Vector3 otherHumanToMe      = transform.position - otherPosition;
                                Vector3 otherPersonMovement = otherRotation * Vector3.right;
                                bool    isToTheRight        = Vector3.Cross(otherPersonMovement, otherHumanToMe).z < 0;

                                Vector3 meetingPosition = transform.position + (otherPosition - transform.position) / 2f;

                                deviationTarget                 = meetingPosition + Quaternion.Euler(0f, 0f, transform.rotation.eulerAngles.z + (isToTheRight ? 90f : -90f)) * BODY_WIDTH;
                                timedDeviationTarget            = null;
                                otherHuman.deviationTarget      = meetingPosition + Quaternion.Euler(0f, 0f, otherRotation.eulerAngles.z + (isToTheRight ? 90f : -90f)) * BODY_WIDTH;
                                otherHuman.timedDeviationTarget = null;
                            }
                        }
                    }
                    else if (colliderName == "BODY")
                    {
                        if (walkPath.Count > 0 && otherHuman.walkPath.Count > 0)
                        {
                            float ourDistance   = Misc.getDistance(transform.position, walkPath [0]);
                            float otherDistance = Misc.getDistance(otherHuman.transform.position, otherHuman.walkPath [0]);
                            if (ourDistance > otherDistance || (ourDistance == otherDistance && !shouldDecide))
                            {
                                if (!otherHuman.waitForHumans.Contains(this))
                                {
                                    waitForHumans.Add(otherHuman);
                                }
                            }
                        }
                    }
                }
            }
        }
    }