Advertisement
otorp2

ship

Jul 15th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1.  
  2. extends Area2D
  3.  
  4.  
  5. const SPEED = 200
  6.  
  7. var screen_size
  8.  
  9. var prev_shooting=false
  10.  
  11. func _process(delta):
  12.  
  13. var motion = Vector2()
  14. if Input.is_action_pressed("move_up"):
  15. motion+=Vector2(0,-1)
  16. if Input.is_action_pressed("move_down"):
  17. motion+=Vector2(0,1)
  18. if Input.is_action_pressed("move_left"):
  19. motion+=Vector2(-1,0)
  20. if Input.is_action_pressed("move_right"):
  21. motion+=Vector2(1,0)
  22. var shooting = Input.is_action_pressed("shoot")
  23.  
  24. var pos = get_pos()
  25.  
  26. pos+=motion*delta*SPEED
  27. if (pos.x<0):
  28. pos.x=0
  29. if (pos.x>screen_size.x):
  30. pos.x=screen_size.x
  31. if (pos.y<0):
  32. pos.y=0
  33. if (pos.y>screen_size.y):
  34. pos.y=screen_size.y
  35.  
  36. set_pos(pos)
  37.  
  38. if (shooting and not prev_shooting):
  39. # just pressed
  40. var shot = preload("res://shot.scn").instance()
  41. #use the position3d as reference
  42. shot.set_pos( get_node("shootfrom").get_global_pos() )
  43. #put it two parents above, so it is not moved by us
  44. get_node("../..").add_child(shot)
  45. #play sound
  46. get_node("sfx").play("shoot")
  47.  
  48.  
  49. prev_shooting = shooting
  50.  
  51. #update points counter
  52. get_node("../hud/score_points").set_text( str(get_node("/root/game_state").points) )
  53.  
  54. func _ready():
  55. # Initialization here
  56. screen_size = get_viewport().get_rect().size
  57. set_process(true)
  58. pass
  59.  
  60. var killed=false
  61.  
  62. func _hit_something():
  63. if (killed):
  64. return
  65. killed=true
  66. get_node("anim").play("explode")
  67. get_node("sfx").play("sound_explode")
  68. get_node("../hud/game_over").show()
  69. get_node("/root/game_state").game_over()
  70. get_parent().stop()
  71. set_process(false)
  72.  
  73.  
  74. func _on_ship_body_enter( body ):
  75. _hit_something()
  76.  
  77.  
  78. func _on_ship_area_enter( area ):
  79. if (area.has_method("is_enemy") and area.is_enemy()):
  80. _hit_something()
  81.  
  82.  
  83. func _on_back_to_menu_pressed():
  84. get_tree().change_scene("res://main_menu.scn")
  85. pass # replace with function body
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement