Advertisement
nitrodog96

PlayerController

Mar 14th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. class PlayerController : ZilchComponent
  2. {
  3. sends ShootUp : ZilchEvent;
  4. sends ShootDown : ZilchEvent;
  5. sends ShootLeft : ZilchEvent;
  6. sends ShootRight : ZilchEvent;
  7. sends ShootUpRight : ZilchEvent;
  8. sends ShootUpLeft : ZilchEvent;
  9. sends ShootDownRight : ZilchEvent;
  10. sends ShootDownLeft : ZilchEvent;
  11.  
  12. [Property]
  13. var MoveUp : Keys = Keys.W;
  14.  
  15. [Property]
  16. var MoveDown : Keys = Keys.S;
  17.  
  18. [Property]
  19. var MoveLeft : Keys = Keys.A;
  20.  
  21. [Property]
  22. var MoveRight : Keys = Keys.D;
  23.  
  24. [Property]
  25. var ShootUp : Keys = Keys.Up;
  26.  
  27. [Property]
  28. var ShootDown : Keys = Keys.Down;
  29.  
  30. [Property]
  31. var ShootLeft : Keys = Keys.Left;
  32.  
  33. [Property]
  34. var ShootRight : Keys = Keys.Right;
  35.  
  36. [Property]
  37. var MovementSpeed : Real = 0.0;
  38.  
  39. [Property]
  40. var StandingSprite : SpriteSource = null;
  41.  
  42. [Property]
  43. var WalkingSprite : SpriteSource = null;
  44.  
  45. var DefaultShotSpeed : Real = 20.0;
  46.  
  47. var ShotSpeedModifier : Real = 1.0;
  48.  
  49. var ShotTimer : Real = 0;
  50.  
  51. var TimerReset : Boolean = false;
  52.  
  53. var BulletDamageMod : Real = 1.0;
  54.  
  55. function Initialize(init : CogInitializer)
  56. {
  57. Zero.Connect(this.Space, Events.LogicUpdate, this.OnLogicUpdate);
  58. }
  59.  
  60. function OnLogicUpdate(event : UpdateEvent)
  61. {
  62. this.TimerReset = false;
  63. if(this.ShotTimer > 0){
  64. this.ShotTimer -= 1;
  65. }
  66.  
  67. var movement = Real3(0,0,0);
  68. var bulletAim = Real3(0,0,0);
  69.  
  70. if(Zero.Keyboard.KeyIsDown(this.MoveUp)){
  71. movement += Real3(0.0, 1.0, 0.0);
  72. }
  73.  
  74. if(Zero.Keyboard.KeyIsDown(this.MoveDown)){
  75. movement += Real3(0.0, -1.0, 0.0);
  76. }
  77.  
  78. if(Zero.Keyboard.KeyIsDown(this.MoveRight)){
  79. movement += Real3(1.0, 0.0, 0.0);
  80. }
  81.  
  82. if(Zero.Keyboard.KeyIsDown(this.MoveLeft)){
  83. movement += Real3(-1.0, 0.0, 0.0);
  84. }
  85.  
  86. if(Zero.Keyboard.KeyIsDown(this.ShootUp)){
  87. if(this.ShotTimer <= 0){
  88. bulletAim += Real3(0.0, 1.0, 0.0);
  89. this.TimerReset = true;
  90. }
  91. }
  92.  
  93. if(Zero.Keyboard.KeyIsDown(this.ShootDown)){
  94. if(this.ShotTimer <= 0){
  95. bulletAim += Real3(0.0, -1.0, 0.0);
  96. this.TimerReset = true;
  97. }
  98. }
  99.  
  100. if(Zero.Keyboard.KeyIsDown(this.ShootLeft)){
  101. if(this.ShotTimer <= 0){
  102. bulletAim += Real3(-1.0, 0.0, 0.0);
  103. this.TimerReset = true;
  104. }
  105. }
  106.  
  107. if(Zero.Keyboard.KeyIsDown(this.ShootRight)){
  108. if(this.ShotTimer <= 0){
  109. bulletAim += Real3(1.0, 0.0, 0.0);
  110. this.TimerReset = true;
  111. }
  112. }
  113.  
  114. if(this.TimerReset){
  115. this.ShotTimer = this.DefaultShotSpeed * this.ShotSpeedModifier;
  116. }
  117.  
  118. if(movement == Real3(0.0, 0.0, 0.0))
  119. {
  120. if(this.Owner.Sprite.SpriteSource != this.StandingSprite)
  121. {
  122. this.Owner.Sprite.SpriteSource = this.StandingSprite;
  123. }
  124. }
  125. else
  126. {
  127. if(this.Owner.Sprite.SpriteSource != this.WalkingSprite)
  128. {
  129. this.Owner.Sprite.SpriteSource = this.WalkingSprite;
  130. }
  131. this.Owner.Transform.Translation += movement * this.MovementSpeed * event.Dt;
  132. }
  133.  
  134. if(bulletAim == Real3(1.0, 0.0, 0.0)){
  135. this.Owner.DispatchEvent(Events.ShootRight, ZilchEvent());
  136. }
  137.  
  138. if(bulletAim == Real3(-1.0, 0.0, 0.0)){
  139. this.Owner.DispatchEvent(Events.ShootLeft, ZilchEvent());
  140. }
  141.  
  142. if(bulletAim == Real3(0.0, 1.0, 0.0)){
  143. this.Owner.DispatchEvent(Events.ShootUp, ZilchEvent());
  144. }
  145.  
  146. if(bulletAim == Real3(0.0, -1.0, 0.0)){
  147. this.Owner.DispatchEvent(Events.ShootDown, ZilchEvent());
  148. }
  149.  
  150. if(bulletAim == Real3(1.0, 1.0, 0.0)){
  151. this.Owner.DispatchEvent(Events.ShootUpRight, ZilchEvent());
  152. }
  153.  
  154. if(bulletAim == Real3(1.0, -1.0, 0.0)){
  155. this.Owner.DispatchEvent(Events.ShootDownRight, ZilchEvent());
  156. }
  157.  
  158. if(bulletAim == Real3(-1.0, 1.0, 0.0)){
  159. this.Owner.DispatchEvent(Events.ShootUpLeft, ZilchEvent());
  160. }
  161.  
  162. if(bulletAim == Real3(-1.0, -1.0, 0.0)){
  163. this.Owner.DispatchEvent(Events.ShootDownLeft, ZilchEvent());
  164. }
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement