/Name/Name/GameObjects/Player/PlayerObject.cs

# · C# · 355 lines · 311 code · 42 blank · 2 comment · 50 complexity · 0be4ff1cdc03bb1f9b2300c064b33721 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Input.Touch;
  11. using Microsoft.Xna.Framework.Media;
  12. using Engine;
  13. using Engine.Core;
  14. namespace Name
  15. {
  16. public class PlayerObject : BaseObjectStreamingHelper<PlayerObject>
  17. {
  18. public static Object ReturnsNew(bool InWorld, float uID)
  19. {
  20. return new PlayerObject(InWorld, uID);
  21. }
  22. public static new ReturnsNew ReturnNew
  23. {
  24. get
  25. {
  26. return ReturnsNew;
  27. }
  28. }
  29. public static string IdleLeft = "IdleLeft";
  30. public static string IdleRight = "IdleRight";
  31. public static string WalkLeft = "WalkLeft";
  32. public static string WalkRight = "WalkRight";
  33. public static string AimLeft = "AimLeft";
  34. public static string AimRight = "AimRight";
  35. public static string ShootLeft = "ShootLeft";
  36. public static string ShootRight = "ShootRight";
  37. Drawable2D drawable;
  38. protected Facing facing;
  39. protected LaunchingRod rod;
  40. protected PlayerHitpoints hp;
  41. protected String myName = "Hired Goon";
  42. public PlayerObject(String name)
  43. : this()
  44. {
  45. myName = name;
  46. }
  47. protected PlayerObject(bool InWorld, float uID)
  48. : base(InWorld, uID)
  49. {
  50. TeamMember gt = new TeamMember(Name.Team.NoTeam);
  51. base.AddComponent(gt);
  52. HitPoints gh = new HitPoints(100);
  53. base.AddComponent(gh);
  54. Placeable p = new Placeable(true, -1);
  55. this.AddComponent(p);
  56. drawable = new Drawable2D(true, -1);
  57. base.AddComponent(drawable);
  58. this.AddComponent(new WeaponHolder(new RocketLauncher(this)));
  59. drawable.DrawLayer = (int)DrawLayer.LayerDepth.Players / 100.0f;
  60. int fps = 3;
  61. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\StandLeft"), IdleLeft);
  62. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\StandRight"), IdleRight);
  63. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\WalkLeft"), new Rectangle(0, 0, 64, 64), WalkLeft, fps);
  64. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\WalkRight"), new Rectangle(0, 0, 64, 64), WalkRight, fps);
  65. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\AimLeft"), AimLeft);
  66. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\AimRight"), AimRight);
  67. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\ShootLeft"), ShootLeft);
  68. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\ShootRight"), ShootRight);
  69. Physics2D phy = new Physics2D(new Rectangle(0, 0, 40, 55));
  70. AddComponent(phy);
  71. System.Random rand = new Random();
  72. if (rand.Next(1, 3) == 1)
  73. {
  74. drawable.PlayAnimation("IdleLeft");
  75. facing = Facing.Left;
  76. }
  77. else
  78. {
  79. drawable.PlayAnimation("IdleRight");
  80. facing = Facing.Right;
  81. }
  82. rod = new LaunchingRod(this);
  83. hp = new PlayerHitpoints(this);
  84. (rod[Drawable2D.TypeStatic] as Drawable2D).Visible = false;
  85. }
  86. public PlayerObject()
  87. {
  88. TeamMember gt = new TeamMember( Name.Team.NoTeam );
  89. base.AddComponent( gt );
  90. HitPoints gh = new HitPoints( 100 );
  91. base.AddComponent( gh );
  92. Placeable p = new Placeable(true,-1);
  93. this.AddComponent( p );
  94. drawable = new Drawable2D(true,-1);
  95. base.AddComponent( drawable );
  96. this.AddComponent( new WeaponHolder( new RocketLauncher( this ) ) );
  97. drawable.DrawLayer = (int) DrawLayer.LayerDepth.Players / 100.0f;
  98. int fps = 3;
  99. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\StandLeft"), IdleLeft);
  100. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\StandRight"), IdleRight);
  101. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>( @"Content\Sprites\Gir\WalkLeft" ), new Rectangle(0, 0, 64, 64), WalkLeft, fps );
  102. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\WalkRight"), new Rectangle(0, 0, 64, 64), WalkRight, fps);
  103. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\AimLeft"), AimLeft);
  104. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\AimRight"), AimRight);
  105. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\ShootLeft"), ShootLeft);
  106. drawable.SetupAnimation(Engine.ContentManager.Instance.GetObject<Texture2D>(@"Content\Sprites\Gir\ShootRight"), ShootRight);
  107. Physics2D phy = new Physics2D( new Rectangle( 0, 0, 40, 55 ) );
  108. AddComponent( phy );
  109. System.Random rand = new Random();
  110. if (rand.Next(1,3) == 1)
  111. {
  112. drawable.PlayAnimation( "IdleLeft" );
  113. facing = Facing.Left;
  114. }
  115. else
  116. {
  117. drawable.PlayAnimation( "IdleRight" );
  118. facing = Facing.Right;
  119. }
  120. rod = new LaunchingRod(this);
  121. hp = new PlayerHitpoints(this);
  122. (rod[Drawable2D.TypeStatic] as Drawable2D).Visible = false;
  123. }
  124. public override void CleanUp( bool RemoveFromObjectList )
  125. {
  126. if ( RemoveFromObjectList )
  127. {
  128. if(hp != null)
  129. hp.CleanUp();
  130. if(rod != null)
  131. rod.CleanUp();
  132. }
  133. hp = null;
  134. rod = null;
  135. base.CleanUp( RemoveFromObjectList );
  136. }
  137. public void RemoveMe()
  138. {
  139. CleanUp();
  140. Engine.GameState.GameStateSystem.Instance.RemoveObjectLater(this);
  141. }
  142. public void Shoot( Facing NewFacing )
  143. {
  144. if ( NewFacing == Engine.Core.Facing.Right )
  145. drawable.PlayAnimation( ShootRight, AimRight);
  146. else if ( NewFacing == Engine.Core.Facing.Left )
  147. drawable.PlayAnimation( ShootLeft, AimLeft );
  148. facing = NewFacing;
  149. }
  150. public void Shoot( )
  151. {
  152. Shoot( facing );
  153. }
  154. public void ShowRod()
  155. {
  156. if(rod != null)
  157. rod.Deploy(facing);
  158. }
  159. public void HideRod()
  160. {
  161. if(rod != null)
  162. rod.Retract(facing);
  163. }
  164. public void ShowWeapon()
  165. {
  166. if ( Weapon != null )
  167. {
  168. Weapon.SetFacing( facing );
  169. Weapon.Visible = true;
  170. }
  171. }
  172. public void HideWeapon()
  173. {
  174. if(Weapon != null)
  175. Weapon.Visible = false;
  176. }
  177. EventManager.GameEvent CurrentDeployEvent;
  178. public void DeployAndShot()
  179. {
  180. Shoot();
  181. // If not Jetpack
  182. if(Weapon is JetPack)
  183. {
  184. ShowWeapon();
  185. }else{
  186. ShowRod();
  187. CurrentDeployEvent = EventManager.Instance.AddEvent( ShowWeapon, 1.25f );
  188. }
  189. // else
  190. }
  191. public void Retract( )
  192. {
  193. Retract( true );
  194. }
  195. public void Retract(bool ThrowEvent )
  196. {
  197. if (Weapon != null && Weapon is JetPack )
  198. {
  199. HideWeapon();
  200. }
  201. else if ( Weapon != null )
  202. {
  203. HideWeapon();
  204. if ( CurrentDeployEvent != null )
  205. {
  206. EventManager.Instance.RemoveEvent( CurrentDeployEvent );
  207. CurrentDeployEvent = null;
  208. }
  209. HideRod();
  210. if ( ThrowEvent )
  211. EventManager.Instance.AddEvent( Stand, drawable.PlayTime / 30.0f );
  212. }
  213. else
  214. {
  215. Engine.DebugHelper.Break( Weapon == null, DebugHelper.DebugLevels.Curious );
  216. }
  217. }
  218. public void Stand( Facing NewFacing )
  219. {
  220. if ( NewFacing == Engine.Core.Facing.Right )
  221. drawable.PlayAnimation( IdleRight );
  222. else if ( NewFacing == Engine.Core.Facing.Left )
  223. drawable.PlayAnimation( IdleLeft );
  224. facing = NewFacing;
  225. }
  226. public void Stand( )
  227. {
  228. Stand( facing );
  229. }
  230. public void Aim( Facing NewFacing )
  231. {
  232. if ( NewFacing == Engine.Core.Facing.Right )
  233. drawable.PlayAnimation( AimRight );
  234. else if ( NewFacing == Engine.Core.Facing.Left )
  235. drawable.PlayAnimation( AimLeft );
  236. facing = NewFacing;
  237. }
  238. public void Aim( )
  239. {
  240. Aim( facing );
  241. }
  242. public void Walk( Facing NewFacing )
  243. {
  244. if(NewFacing == Engine.Core.Facing.Right)
  245. drawable.PlayAnimation( WalkRight );
  246. else if ( NewFacing == Engine.Core.Facing.Left )
  247. drawable.PlayAnimation( WalkLeft );
  248. facing = NewFacing;
  249. }
  250. public void Walk( )
  251. {
  252. Walk( facing );
  253. }
  254. public Facing Facing
  255. {
  256. get
  257. {
  258. return facing;
  259. }
  260. }
  261. public String MyName
  262. {
  263. set
  264. {
  265. myName = value;
  266. }
  267. get
  268. {
  269. return myName;
  270. }
  271. }
  272. public String Team
  273. {
  274. get
  275. {
  276. return (this[TeamMember.TypeStatic] as TeamMember).Team;
  277. }
  278. }
  279. public Vector2 Position
  280. {
  281. get
  282. {
  283. return ( this[Placeable.TypeStatic] as Placeable ).Get2DPosition();
  284. }
  285. }
  286. public void SwapWeapon( IWeapon w )
  287. {
  288. ( this[WeaponHolder.TypeStatic] as WeaponHolder ).Weapon = w;
  289. }
  290. public PlayerHitpoints HP
  291. {
  292. get
  293. {
  294. return hp;
  295. }
  296. }
  297. public IWeapon Weapon
  298. {
  299. get
  300. {
  301. WeaponHolder wh = (this[WeaponHolder.TypeStatic] as WeaponHolder );
  302. if ( wh != null )
  303. return wh.Weapon;
  304. else
  305. return null;
  306. }
  307. set
  308. {
  309. SwapWeapon(value);
  310. }
  311. }
  312. }
  313. }