1. using DeltaEngine.Commands;
  2. using DeltaEngine.Datatypes;
  3. using DeltaEngine.Entities;
  4. using DeltaEngine.Input;
  5. using DeltaEngine.Rendering2D;
  6. using DeltaEngine.ScreenSpaces;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. namespace FirstBattleRpgOpenTk.AbstractClass
  12. {
  13. public class Humanoïd : Sprite, Updateable
  14. {
  15. #region # Properties #
  16. private int life;
  17. private int mana;
  18. public int Life
  19. {
  20. get { return life; }
  21. set { life = value; }
  22. }
  23. public int Mana
  24. {
  25. get { return mana; }
  26. set { mana = value; }
  27. }
  28. #endregion
  29. float coeff_densite = 0.5f; // density
  30. float coeff_frottement = 1.5f; // friction
  31. float gravite = 0.981f;
  32. public Vector2D Acceleration;
  33. public Vector2D Velocity;
  34. public bool jumping = false; // he jumps ?
  35. private Game game; // for access a debug label (temporary)
  36. private Vector2D positionBase; // base position
  37. public Humanoïd(Game game, Vector2D position)
  38. : base(new DeltaEngine.Content.Material(new Size(10, 30), Color.LightGray), new Rectangle(position.X, position.Y, 0.05f, 0.08f))
  39. {
  40. this.game = game;
  41. this.positionBase = position;
  42. Displacement();
  43. }
  44. public virtual void Displacement()
  45. {
  46. //var data = new SimplePhysics.Data { Gravity = new Vector2D(0.0f, 0.1f), Duration = 10 };
  47. //Add(data);
  48. //Start<SimplePhysics.Move>();
  49. new Command(Command.MoveLeft, () => MoveLeft());
  50. new Command(Command.MoveRight, () => MoveRight());
  51. new Command(TestJump).Add(new KeyTrigger(Key.Space, State.Pressing));
  52. }
  53. public void TestJump()
  54. {
  55. if (!jumping)
  56. {
  57. Velocity = new Vector2D(0f, 1f); // reset base speed
  58. Acceleration = Vector2D.Zero; // reset acceleration
  59. jumping = true; // he jumps
  60. }
  61. }
  62. public virtual void MoveLeft()
  63. {
  64. if (this.TopLeft.X <= 0)
  65. Center = new Vector2D(Center.X, Center.Y);
  66. else
  67. Center -= new Vector2D(Time.Delta * 0.5f, 0);
  68. }
  69. public virtual void MoveRight()
  70. {
  71. if (this.TopLeft.X + this.Size.Width >= ScreenSpace.Current.Right - 0.02f)
  72. Center = new Vector2D(Center.X, Center.Y);
  73. else
  74. Center += new Vector2D(Time.Delta * 0.5f, 0);
  75. }
  76. public virtual void Update()
  77. {
  78. game.SetDebug("velocity : " + Velocity + " | pos : " + Center + " | acc : " + Acceleration + " | jump : " + jumping);
  79. if (jumping)
  80. {
  81. if (Acceleration.Y >= 0)
  82. {
  83. this.Acceleration = new Vector2D(0, gravite * coeff_densite - coeff_frottement * Velocity.Y);
  84. this.Velocity += this.Acceleration * Time.Delta;
  85. Center -= new Vector2D(0, this.Velocity.Y * Time.Delta);
  86. }
  87. else
  88. {
  89. if (Math.Round(Center.Y, 2) <= positionBase.Y)
  90. {
  91. this.Velocity -= new Vector2D(0f, gravite + coeff_densite) * Time.Delta;
  92. if (Math.Round(Center.Y, 2) > positionBase.Y) // i try to limit the fall to the y base position, but it's not work..
  93. Center = new Vector2D(0, positionBase.Y);
  94. Center -= new Vector2D(0, this.Velocity.Y * Time.Delta);
  95. }
  96. else
  97. {
  98. jumping = false;
  99. }
  100. }
  101. }
  102. }
  103. public bool IsPauseable { get { return true; } }
  104. }
  105. }