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