internal static void AdjustCharacterPosition(int id, float x, float z, double time, float dir) { CharacterInfo info = WorldSystem.Instance.GetCharacterById(id); if (null != info) { MovementStateInfo msi = info.GetMovementStateInfo(); if (time < 1000) { Vector3 pos = msi.GetPosition3D(); double speed = info.GetActualProperty().MoveSpeed; double distance = (speed * time) / 1000; double len = pos.Length(); float nz = (float)(z + distance * Math.Cos(dir)); float nx = (float)(x + distance * Math.Sin(dir)); float dx = nx - pos.X; float dz = nz - pos.Z; float distSqr = dx * dx + dz * dz; if (distSqr > 0) { msi.SetPosition2D(x, z); CharacterView view = EntityManager.Instance.GetCharacterViewById(info.GetId()); if (null != view) { GfxSystem.UpdateGameObjectLocalPosition2D(view.Actor, x, z); } //LogSystem.Debug("PositionController start, id:{0} dx:{1} dz:{2} time:{3}, just move to pos", id, dx, dz, time); } else { CharacterView view = EntityManager.Instance.GetCharacterViewById(info.GetId()); if (null != view) { lock (GfxSystem.SyncLock) { view.ObjectInfo.AdjustDx = dx; view.ObjectInfo.AdjustDz = dz; view.ObjectInfo.CurTime = 0.0f; view.ObjectInfo.TotalTime = 0.5f; } } //LogSystem.Debug("PositionController start, id:{0} dx:{1} dz:{2} time:{3}", id, dx, dz, time); } } else { msi.SetPosition2D(x, z); CharacterView view = EntityManager.Instance.GetCharacterViewById(info.GetId()); if (null != view) { GfxSystem.UpdateGameObjectLocalPosition2D(view.Actor, x, z); } //LogSystem.Debug("PositionController just move to pos, id:{0} x:{1} z:{2}", id, x, z); } } }
private void MoveUser(UserInfo obj, long deltaTime) { MovementStateInfo msi = obj.GetMovementStateInfo(); if (null != obj.ControlledObject) { MovementStateInfo ctrlMsi = obj.ControlledObject.GetMovementStateInfo(); ctrlMsi.IsMoving = msi.IsMoving; ctrlMsi.SetFaceDir(msi.GetFaceDir()); ctrlMsi.SetMoveDir(msi.GetMoveDir()); } if (obj.IsHaveStateFlag(CharacterState_Type.CST_Sleep) || obj.IsHaveStateFlag(CharacterState_Type.CST_FixedPosition)) { return; } //玩家移动中忽略阻挡,由客户端与AI来规划路径。 if (!obj.IsDead() && msi.IsMoving && !msi.IsSkillMoving && !msi.IsMoveMeetObstacle) { Vector3 pos = msi.GetPosition3D(); float speed = (float)obj.GetRealControlledObject().GetActualProperty().MoveSpeed *(float)obj.VelocityCoefficient; float distance = (speed * (float)(int)deltaTime) / 1000.0f; //LogSystem.Debug("MovementSystem user:{0} speed:{1} deltaTime:{2} distance:{3}", obj.GetId(), speed, deltaTime, distance); float x = 0, y = 0; Vector2 newPos = new Vector2(); if (obj.GetAiStateInfo().CurState != (int)AiStateId.Invalid && msi.CalcDistancSquareToTarget() < distance * distance) { x = msi.TargetPosition.X; y = msi.TargetPosition.Z; AdjustPosition(obj.SpatialSystem, ref x, ref y); newPos = new Vector2(x, y); msi.SetPosition2D(newPos); } else { float cosV = (float)msi.MoveDirCosAngle; float sinV = (float)msi.MoveDirSinAngle; y = pos.Z + distance * cosV; x = pos.X + distance * sinV; AdjustPosition(obj.SpatialSystem, ref x, ref y); newPos = new Vector2(x, y); msi.SetPosition2D(newPos); } if (null != obj.ControlledObject) { obj.ControlledObject.GetMovementStateInfo().SetPosition2D(newPos); } } }
protected void UpdateMovement() { CharacterInfo obj = GetOwner(); if (null != obj && !obj.IsDead() && null != ObjectInfo) { if (obj.IsNpc && !obj.CastNpcInfo().CanMove) { return; } MovementStateInfo msi = obj.GetMovementStateInfo(); ObjectInfo.FaceDir = msi.GetFaceDir(); ObjectInfo.WantFaceDir = msi.GetWantFaceDir(); if (msi.IsMoving) { Vector3 pos = msi.GetPosition3D(); ObjectInfo.MoveCos = (float)msi.MoveDirCosAngle; ObjectInfo.MoveSin = (float)msi.MoveDirSinAngle; ObjectInfo.MoveSpeed = (float)obj.GetActualProperty().MoveSpeed *(float)obj.VelocityCoefficient; if (obj is UserInfo) { if (msi.TargetPosition.LengthSquared() < Geometry.c_FloatPrecision) { ObjectInfo.MoveTargetDistanceSqr = 100.0f; } else { ObjectInfo.MoveTargetDistanceSqr = msi.CalcDistancSquareToTarget(); } } else { ObjectInfo.MoveTargetDistanceSqr = msi.CalcDistancSquareToTarget(); } ObjectInfo.IsLogicMoving = true; } else { ObjectInfo.IsLogicMoving = false; } } else { ObjectInfo.IsLogicMoving = false; } }
internal void Create(NpcInfo npc) { Init(); if (null != npc) { m_Npc = npc; ObjectInfo.UnitId = m_Npc.GetUnitId(); ObjectInfo.CampId = m_Npc.GetCampId(); ObjectInfo.LinkId = m_Npc.GetLinkId(); MovementStateInfo msi = m_Npc.GetMovementStateInfo(); Vector3 pos = msi.GetPosition3D(); float dir = msi.GetFaceDir(); CreateActor(m_Npc.GetId(), m_Npc.GetModel(), pos, dir, m_Npc.Scale); npc.ActorId = Actor; ObjectInfo.IsNpc = true; CreateBornEffect(Actor, npc.GetBornEffect(), npc.ParticleScale, npc.GetBornEffectTime()); InitAnimationSets(); } }
private void MoveNpc(NpcInfo obj, long deltaTime) { if (obj.IsHaveStateFlag(CharacterState_Type.CST_Sleep) || obj.IsHaveStateFlag(CharacterState_Type.CST_FixedPosition) || null != obj.ControllerObject) { return; } MovementStateInfo msi = obj.GetMovementStateInfo(); //npc执行移动时忽略阻挡与避让,这些行为由ai模块在规划其路径时执行。 if (!obj.IsDead() && obj.CanMove && msi.IsMoving && !msi.IsSkillMoving) { Vector3 pos = msi.GetPosition3D(); float cos_angle = (float)msi.MoveDirCosAngle; float sin_angle = (float)msi.MoveDirSinAngle; float speed = (float)obj.GetActualProperty().MoveSpeed *(float)obj.VelocityCoefficient; float distance = (speed * (float)(int)deltaTime) / 1000.0f; //LogSystem.Debug("MovementSystem npc:{0} speed:{1} deltaTime:{2} distance:{3}", obj.GetId(), speed, deltaTime, distance); float x = 0, y = 0; if (msi.CalcDistancSquareToTarget() < distance * distance) { x = msi.TargetPosition.X; y = msi.TargetPosition.Z; AdjustPosition(obj.SpatialSystem, ref x, ref y); Vector2 newPos = new Vector2(x, y); msi.SetPosition2D(newPos); } else { float len = pos.Length(); y = pos.Z + distance * cos_angle; x = pos.X + distance * sin_angle; AdjustPosition(obj.SpatialSystem, ref x, ref y); Vector2 newPos = new Vector2(x, y); msi.SetPosition2D(newPos); } } }
internal void Create(UserInfo user) { Init(); if (null != user) { m_User = user; ObjectInfo.UnitId = m_User.GetUnitId(); ObjectInfo.CampId = m_User.GetCampId(); ObjectInfo.LinkId = m_User.GetLinkId(); MovementStateInfo msi = m_User.GetMovementStateInfo(); Vector3 pos = msi.GetPosition3D(); float dir = msi.GetFaceDir(); CreateActor(m_User.GetId(), m_User.GetModel(), pos, dir, m_User.Scale); user.ActorId = Actor; UpdateXSoulEquip(); InitAnimationSets(); ObjectInfo.IsPlayer = true; if (user.GetId() == WorldSystem.Instance.PlayerSelfId) { GfxSystem.MarkPlayerSelf(Actor); } } }
internal void Tick() { long now = TimeUtility.GetServerMilliseconds(); m_LastTickIntervalMs = now - m_LastTickTime; m_LastTickTime = now; if (WorldSystem.Instance.IsObserver && !WorldSystem.Instance.IsFollowObserver) { bool keyPressed = false; float x = 0.5f, y = 0.5f; if (GfxSystem.IsKeyPressed(Keyboard.Code.A)) { x = 0.1f; keyPressed = true; } else if (GfxSystem.IsKeyPressed(Keyboard.Code.D)) { x = 0.9f; keyPressed = true; } if (GfxSystem.IsKeyPressed(Keyboard.Code.W)) { y = 0.1f; keyPressed = true; } else if (GfxSystem.IsKeyPressed(Keyboard.Code.S)) { y = 0.9f; keyPressed = true; } if (keyPressed) { WorldSystem.Instance.UpdateObserverCamera(x, y); } return; } UserInfo playerself = WorldSystem.Instance.GetPlayerSelf(); if (null == playerself) { return; } // if move input is disable // MotionStatus is MoveStop, and MotionChanged is reflect the change accordingly if (EnableMoveInput) { if (!IsKeyboardControl()) { CheckJoystickControl(); } } if (!m_IsJoystickControl) { pm_.Update(EnableMoveInput); } MovementStateInfo msi = playerself.GetMovementStateInfo(); Vector3 pos = msi.GetPosition3D(); //LogSystem.Debug("Pos : {0}, Dir : {1}", pos.ToString(), playerself.GetMovementStateInfo().GetFaceDir()); bool reface = false; if (m_LastTickIsSkillMoving && !msi.IsSkillMoving) { reface = true; } //操作同步机制改为发给服务器同时本地就开始执行(服务器转发给其它客户端,校验失败则同时发回原客户端进行位置调整) Vector3 mouse_pos = new Vector3(GfxSystem.GetMouseX(), GfxSystem.GetMouseY(), GfxSystem.GetMouseZ()); if (pm_.MotionStatus == PlayerMovement.Motion.Moving || pm_.JoyStickMotionStatus == PlayerMovement.Motion.Moving) { if (pm_.MotionChanged || pm_.JoyStickMotionChanged || !m_LastTickIsMoving) { StopAiMove(); playerself.SkillController.AddBreakSkillTask(); float moveDir = RoundMoveDir(pm_.MoveDir); //GfxSystem.GfxLog("PlayerControl.Tick MoveDir:{0} RoundMoveDir:{1}", pm_.MoveDir, moveDir); if (!m_LastTickIsMoving || !Geometry.IsSameFloat(moveDir, m_lastMoveDir)) { msi.SetMoveDir(moveDir); //ControlSystemOperation.AdjustCharacterMoveDir(playerself.GetId(), moveDir); msi.IsMoving = true; msi.TargetPosition = Vector3.Zero; if (WorldSystem.Instance.IsPvpScene() || WorldSystem.Instance.IsMultiPveScene()) { NetworkSystem.Instance.SyncPlayerMoveStart(moveDir); } } if (EnableRotateInput) { if (reface || !m_LastTickIsMoving || !Geometry.IsSameFloat(pm_.MoveDir, m_lastDir)) { msi.SetFaceDir(pm_.MoveDir); //ControlSystemOperation.AdjustCharacterFaceDir(playerself.GetId(), pm_.MoveDir); msi.SetWantFaceDir(pm_.MoveDir); if (WorldSystem.Instance.IsPvpScene() || WorldSystem.Instance.IsMultiPveScene()) { NetworkSystem.Instance.SyncFaceDirection(pm_.MoveDir); } } } m_lastDir = pm_.MoveDir; m_lastMoveDir = moveDir; } m_LastTickIsMoving = true; } else { if (m_LastTickIsMoving) { playerself.SkillController.CancelBreakSkillTask(); playerself.GetMovementStateInfo().IsMoving = false; if (WorldSystem.Instance.IsPvpScene() || WorldSystem.Instance.IsMultiPveScene()) { NetworkSystem.Instance.SyncPlayerMoveStop(); } if (EnableRotateInput) { if (reface) { msi.SetFaceDir(m_lastDir); //ControlSystemOperation.AdjustCharacterFaceDir(playerself.GetId(), m_lastDir); msi.SetWantFaceDir(m_lastDir); if (WorldSystem.Instance.IsPvpScene() || WorldSystem.Instance.IsMultiPveScene()) { NetworkSystem.Instance.SyncFaceDirection(m_lastDir); } } } } m_LastTickIsMoving = false; } m_LastTickIsSkillMoving = msi.IsSkillMoving; old_mouse_pos_ = mouse_pos_; mouse_pos_.X = GfxSystem.GetMouseX(); mouse_pos_.Y = GfxSystem.GetMouseY(); UserAiStateInfo aiInfo = playerself.GetAiStateInfo(); if (null != aiInfo && (int)AiStateId.Idle == aiInfo.CurState) { m_lastSelectObjId = -1; } }