- Timestamp:
- 2013-07-23 22:36:26 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Units/Unit.cs
r4581 r4592 14 14 public class Unit : Entity 15 15 { 16 protected bool isSelectable , isSelected;16 protected bool isSelectable; 17 17 protected BoundingRectangle screenRect; 18 18 protected BoundingRectangle screenRectOffset; … … 22 22 private Vector2 arrowOffs = Vector2.Zero; 23 23 private Player owner; 24 private int immunityTimer = 0; 25 protected int maxHealth, immunityTime; 26 protected Color colorizer = Color.White; 27 protected bool colorizeOnHit, wasHurt = false; 24 28 25 29 public Unit(Level level, Player owner) … … 31 35 screenRectOffset = new BoundingRectangle(0, 0, spriteSize.X, spriteSize.Y, null); 32 36 isSelectable = true; 33 isSelected = false; 37 IsSelected = false; 38 HasHealth = false; 39 maxHealth = 10; 40 colorizeOnHit = true; 41 immunityTime = 0; 34 42 } 43 44 public int Health { get; protected set; } 45 46 public bool HasHealth { get; protected set; } 35 47 36 48 public bool IsSelectable { get { return isSelectable; } } … … 38 50 public Player Owner { get { return owner; } } 39 51 52 public bool IsSelected { get; set; } 53 40 54 public override void Init() 41 55 { 42 56 base.Init(); 57 Health = maxHealth; 43 58 } 44 59 45 public virtual void OnSelectGain()60 public virtual bool CanBeHurtBy(Unit u) 46 61 { 47 isSelected = true;62 return HasHealth && u.Owner != Owner; 48 63 } 49 64 50 public v irtual void OnSelectLost()65 public void Hit(Unit u, int damage, float dir, float pushPower) 51 66 { 52 isSelected = false; 67 if (u.CanBeHurtBy(this)) 68 u.OnDamage(this, damage, dir, pushPower); 69 if (u.Health <= 0) 70 u.Remove(); 53 71 } 54 72 55 public virtual void OnFunctionClick() { } 73 public virtual void OnDamage(Unit attacker, int damage, float dir, float pushPower) 74 { 75 Health -= damage; 76 if (colorizeOnHit) 77 colorizer = Color.Red; 78 if (immunityTime > 0) 79 { 80 wasHurt = true; 81 HasHealth = false; 82 } 83 } 84 85 public virtual void OnFunctionClick(float x, float z) { } 56 86 57 87 public override void Update() 58 88 { 59 if ( isSelected)89 if (IsSelected) 60 90 { 61 91 timer += arrowSpeed; 62 92 if (timer >= MathHelper.TwoPi) timer = -MathHelper.TwoPi; 63 93 arrowOffs.Y = (float)Math.Sin(timer) * 2.0F; 94 } 95 96 if (wasHurt) 97 { 98 immunityTimer++; 99 if (immunityTimer >= immunityTime) 100 { 101 wasHurt = false; 102 immunityTimer = 0; 103 colorizer = Color.White; 104 HasHealth = true; 105 } 64 106 } 65 107 } … … 80 122 } 81 123 124 public float DistanceToSqr(float x, float z) 125 { 126 float xd = X - x; 127 float zd = Z - z; 128 129 return xd * xd + zd * zd; 130 } 131 82 132 public float DistanceFromScreenSpaceSqr(float x, float y) 83 133 { … … 95 145 public override void Render(RenderHelper renderer) 96 146 { 97 if ( isSelected)147 if (IsSelected) 98 148 RenderHighLight(renderer); 99 149 }
Note: See TracChangeset
for help on using the changeset viewer.