Advertisement
CodeTortoise

soldier.gd

Dec 18th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1.  
  2. extends KinematicBody2D
  3.  
  4. var dialog
  5. var collider
  6. var SoldierRay
  7. var npc
  8. var has_response = false
  9.  
  10. var is_action_pressed = false
  11.  
  12. export var walk = 200
  13. export var run  = 300
  14.  
  15. var old_motion = Vector2()
  16. var anim = "down_idle"
  17. var timelapse = 0
  18.  
  19.  
  20. func process_movement(delta):
  21.     var motion = Vector2(0,0)
  22.    
  23.     if Input.is_action_pressed("move_up"):
  24.         motion += Vector2(0, -1)
  25.     if Input.is_action_pressed("move_down"):
  26.         motion += Vector2(0, 1)
  27.     if Input.is_action_pressed("move_left"):
  28.         motion += Vector2(-1, 0)
  29.     if Input.is_action_pressed("move_right"):
  30.         motion += Vector2(1, 0)
  31.        
  32.     if Input.is_action_pressed("ui_cancel"):
  33.         get_tree().change_scene("scenes/menu.scn")
  34.        
  35.     var velocity = walk
  36.     if Input.is_action_pressed("run_mod"):
  37.         velocity = run
  38.    
  39.     motion = motion.normalized() * velocity * delta
  40.     move(motion)
  41.    
  42.     timelapse += delta
  43.    
  44.     receive_dialog()
  45.    
  46.     # animation changes
  47.     if (old_motion == motion):
  48.         return
  49.        
  50.     old_motion = motion
  51.     if(motion == Vector2(0,0)):
  52.         anim += ("_idle")
  53.     elif(motion.x < 0):
  54.         anim = "left"
  55.         get_node("RayCast2D").set_rot(3*PI/2)
  56.     elif(motion.x > 0):
  57.         anim = "right"
  58.         get_node("RayCast2D").set_rot((PI)/2)
  59.     elif(motion.y < 0):
  60.         anim = "up"
  61.         get_node("RayCast2D").set_rot(PI)
  62.     elif(motion.y > 0):
  63.         anim = "down"
  64.         get_node("RayCast2D").set_rot(0)
  65.        
  66.     get_node("walk-anim").play(anim)
  67.  
  68.  
  69. func receive_dialog():
  70.     if SoldierRay.is_colliding() and Input.is_action_pressed("action"):
  71.         if is_action_pressed == false:
  72.             npc = SoldierRay.get_collider()
  73.             print(npc.get_name()) # console check
  74.             has_response = npc.has_node("Response")
  75.             print(has_response) # console check
  76.             if has_response:
  77.                 dialog = npc.get_node("Response").get_text()
  78.                 print(dialog) # console check
  79.                 return dialog
  80.             is_action_pressed = true
  81.     else:
  82.         is_action_pressed = false
  83.  
  84.  
  85. func _ready():
  86.     set_process(true)
  87.     set_fixed_process(true)
  88.     SoldierRay = get_node("RayCast2D")
  89.     SoldierRay.add_exception(self)
  90.    
  91.    
  92. func _fixed_process(delta):
  93.     process_movement(delta)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement