/Shared/Shared/Objects/PlayerCreatureBase.cs

https://bitbucket.org/arcanosteam/arcanos · C# · 67 lines · 56 code · 9 blank · 2 comment · 10 complexity · 2df81be761a6ea0e559755b2b8ef006f MD5 · raw file

  1. using System;
  2. using Arcanos.Shared.Collisions;
  3. using Arcanos.Shared.Grids;
  4. using Arcanos.Shared.Movement;
  5. using Arcanos.Shared.Players;
  6. using Microsoft.Xna.Framework;
  7. namespace Arcanos.Shared.Objects
  8. {
  9. public abstract class PlayerCreatureBase : CreatureBase
  10. {
  11. private const float MOVEMENT_DISTANCE_TOLERANCE_PERCENT = 1.1f;
  12. private const float STATIC_COLLISION_CHECK_DISTANCE = 10.0f;
  13. public static int FriendlyTargetDecalTextureID;
  14. public static int NeutralTargetDecalTextureID;
  15. public static int HostileTargetDecalTextureID;
  16. protected readonly CharacterBase character;
  17. public CharacterBase Character
  18. {
  19. get { return character; }
  20. }
  21. protected PlayerCreatureBase(ulong guid, CreatureTemplateBase template, CharacterBase character, Vector3 pos, Rotation rot) : base(guid, template, pos, rot)
  22. {
  23. Type = ObjectType.PlayerCreature;
  24. this.character = character;
  25. }
  26. public MovementValidity IsMovementValid(Vector3 source, Vector3 destination, bool clientside)
  27. {
  28. if (HasFlag(ObjectFlags.Stunned))
  29. return MovementValidity.Incapable;
  30. float speed = GetMovementSpeed();
  31. if (Vector3.Distance(source, destination) > speed * MOVEMENT_DISTANCE_TOLERANCE_PERCENT)
  32. return MovementValidity.MovedTooFar;
  33. if (clientside)
  34. {
  35. // Terrain collisions check
  36. if (destination.Z - source.Z >= 0.5f)
  37. return MovementValidity.CollisionWithTerrain;
  38. if (Map.Template.GetNormal(destination.X, destination.Y).Z <= 0.75f && destination.Z >= source.Z)
  39. return MovementValidity.SlopeTooSteep;
  40. }
  41. // SMO collisions check
  42. GridBounds bounds = Map.Grid.GetGridBounds(Position, STATIC_COLLISION_CHECK_DISTANCE);
  43. for (int x = bounds.MinX; x <= bounds.MaxX; ++x)
  44. for (int y = bounds.MinY; y <= bounds.MaxY; ++y)
  45. foreach (StaticModelObjectBase smo in Map.Grid.Cells[x][y].SolidStatics)
  46. switch (smo.Shape)
  47. {
  48. case Shape.Cylinder:
  49. if (Math.Sqrt((destination.X - smo.Position.X) * (destination.X - smo.Position.X) + (destination.Y - smo.Position.Y) * (destination.Y - smo.Position.Y)) <= GetApproachRadius() + smo.GetApproachRadius())
  50. return MovementValidity.CollisionWithSolidStatic;
  51. break;
  52. default:
  53. throw new ArgumentOutOfRangeException();
  54. }
  55. return MovementValidity.Ok;
  56. }
  57. }
  58. }