Exemplo n.º 1
0
        public void RemoveGameObject(NetGameObject inGameObject)
        {
            int index = inGameObject.GetIndexInWorld();

            int lastIndex = mGameObjects.Count - 1;

            if (index != lastIndex)
            {
                mGameObjects[index] = mGameObjects[lastIndex];
                mGameObjects[index].SetIndexInWorld(index);
            }

            inGameObject.SetIndexInWorld(-1);

            mGameObjects.RemoveAt(lastIndex);

            inGameObject.CompleteRemove();

            //mWorldMap.RemoveObject(inGameObject);

            if (inGameObject.GetMapId() != 0)
            {
                RemoveNetGameObject((GameObjectClassId)inGameObject.GetClassId(), inGameObject.GetMapId());
            }
        }
Exemplo n.º 2
0
        private bool Collision(float sourceRadius, Vector3 sourceLocation, NetGameObject target)
        {
            if (target.GetNetworkId() != this.GetNetworkId() && !target.DoesWantToDie())
            {
                // 같은 층이 아닌 경우는 충돌 검사에서 제외
                if (target.DetectCollision(sourceRadius, sourceLocation) && IsSameFloor(target))
                {
                    //first, tell the other guy there was a collision with a cat, so it can do something...
                    if (target.HandleCollisionWithActor(this))
                    {
#if _USE_INPUT_SYNC
                        //simple collision test for spheres- are the radii summed less than the distance?
                        Vector3 targetLocation = target.GetLocation();
                        float   targetRadius   = target.GetCollisionRadius();

                        Vector3 delta         = targetLocation - sourceLocation;
                        float   distSq        = delta.sqrMagnitude;
                        float   collisionDist = (sourceRadius + targetRadius);
                        //if (distSq < (collisionDist * collisionDist))
                        collisionDist = 1.02f;


#if UNITY_EDITOR || DEBUG
                        LogHelper.LogInfo($"collision source{sourceLocation}, target{targetLocation}, delta{delta}, dist{collisionDist}, mapID{target.GetMapId()}, objectCount{World.Instance(WorldId).GetGameObjects().Count+ World.mStaticGameObjects.Count}");
                        LogHelper.LogDrawRay(sourceLocation, delta, new Vector3(1, 0, 1), 1);
                        Vector3 startPos = target.GetLocation() + Vector3.up * (target.rh * 0.5f) + Vector3.left * 0.5f + Vector3.back * (0.5f + target.rh - 1);
                        LogHelper.LogDrawLine(startPos, startPos + Vector3.right * (target.rw), new Vector3(1, 1, 1), 1);
                        LogHelper.LogDrawLine(startPos, startPos + Vector3.forward * (target.rh), new Vector3(1, 1, 1), 1);
                        startPos = target.GetLocation() + Vector3.up * (target.rh * 0.5f) + Vector3.right * (0.5f + target.rw - 1) + Vector3.forward * 0.5f;
                        LogHelper.LogDrawLine(startPos, startPos + Vector3.left * (target.rw), new Vector3(1, 1, 1), 1);
                        LogHelper.LogDrawLine(startPos, startPos + Vector3.back * (target.rh), new Vector3(1, 1, 1), 1);
#endif

#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_IOS || UNITY_ANDROID
                        if (distSq < ((targetLocation - lastLocation).sqrMagnitude - 0.01f))
                        {
                            LogHelper.LogInfo($"cur {distSq}, last {(targetLocation - lastLocation).sqrMagnitude}");
                            SolveCollision(sourceRadius, sourceLocation, target.GetLocation().x, target.GetLocation().z, target.rw, target.rh);
                        }
                        return(true);
#endif

                        //okay, you hit something!
                        //so, project your location far enough that you're not colliding
                        Vector3 dirToTarget = delta;
                        dirToTarget.Normalize();
                        Vector3 acceptableDeltaFromSourceToTarget = dirToTarget * collisionDist;
                        //important note- we only move this cat. the other cat can take care of moving itself
                        SetLocation(targetLocation - acceptableDeltaFromSourceToTarget);

                        // Collision 크기가 1보다 큰 경우 처리
                        if (target.rw > 1 || target.rh > 1)
                        {
                            Vector3 movePosRW = Vector3.zero;
                            Vector3 movePosRH = Vector3.zero;

                            if (target.rw > 1 && sourceLocation.x >= targetLocation.x + 0.5f)
                            {
                                movePosRW = Vector3.right;
                            }
                            if (target.rh > 1 && sourceLocation.z >= targetLocation.z + 0.5f)
                            {
                                movePosRH = Vector3.forward;
                            }

                            Vector3 deltaRect       = (targetLocation + movePosRW + movePosRH) - sourceLocation;
                            Vector3 dirToTargetRect = deltaRect;
                            dirToTargetRect.Normalize();
                            Vector3 acceptableDeltaFromSourceToTargetRect = dirToTargetRect * collisionDist;
                            SetLocation((targetLocation + movePosRW + movePosRH) - acceptableDeltaFromSourceToTargetRect);
#if UNITY_EDITOR || DEBUG
                            LogHelper.LogDrawRay(sourceLocation, deltaRect, new Vector3(0, 1, 1), 1);
                            startPos = target.GetLocation() + Vector3.up * (target.rh * 0.5f) + Vector3.left * 0.5f + Vector3.back * (0.5f + target.rh - 1);
                            LogHelper.LogDrawLine(startPos, startPos + Vector3.right * (target.rw), new Vector3(1, 1, 1), 1);
                            LogHelper.LogDrawLine(startPos, startPos + Vector3.forward * (target.rh), new Vector3(1, 1, 1), 1);
                            startPos = target.GetLocation() + Vector3.up * (target.rh * 0.5f) + Vector3.right * (0.5f + target.rw - 1) + Vector3.forward * 0.5f;
                            LogHelper.LogDrawLine(startPos, startPos + Vector3.left * (target.rw), new Vector3(1, 1, 1), 1);
                            LogHelper.LogDrawLine(startPos, startPos + Vector3.back * (target.rh), new Vector3(1, 1, 1), 1);
#endif
                        }


                        Vector3 relVel = mVelocity;

                        //if other object is a cat, it might have velocity, so there might be relative velocity...
                        Actor targetActor = target.GetAsActor();
                        if (targetActor != null)
                        {
                            relVel -= targetActor.mVelocity;
                        }

                        //got vel with dir between objects to figure out if they're moving towards each other
                        //and if so, the magnitude of the impulse ( since they're both just balls )
                        float relVelDotDir = Vector3.Dot(relVel, dirToTarget);

                        if (relVelDotDir > 0.0f)
                        {
                            Vector3 impulse = relVelDotDir * dirToTarget;

                            if (targetActor != null)
                            {
                                mVelocity -= impulse;
                                mVelocity *= mActorRestitution;
                            }
                            else
                            {
                                mVelocity -= impulse * 2.0f;
                                mVelocity *= mWallRestitution;
                            }
                        }

                        return(true);
#else
                        // Solve collision disable
                        // LogHelper.LogInfo($"collision source{sourceLocation}, objectCount{World.Instance(WorldId).GetGameObjects().Count + World.mStaticGameObjects.Count}");
                        return(true);
#endif
                    }
                }
                else
                {
                    target.HandleExitCollisionWithActor(this);
                }
            }

            return(false);
        }