Advertisement
CodeTortoise

Player with raycast

Dec 6th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. #var global
  4. var dialog
  5. var collider
  6.  
  7. export var walk = 200
  8. export var run  = 300
  9.  
  10. var old_motion = Vector2()
  11. var anim = "down_idle"
  12. var timelapse = 0
  13.  
  14.  
  15. func process_movement(delta):
  16.     var motion = Vector2(0,0)
  17.    
  18.     if Input.is_action_pressed("move_up"):
  19.         motion += Vector2(0, -1)
  20.        
  21.     if Input.is_action_pressed("move_down"):
  22.         motion += Vector2(0, 1)
  23.  
  24.     if Input.is_action_pressed("move_left"):
  25.         motion += Vector2(-1, 0)
  26.  
  27.     if Input.is_action_pressed("move_right"):
  28.         motion += Vector2(1, 0)
  29.        
  30.     if Input.is_action_pressed("ui_cancel"):
  31.         get_tree().change_scene("scenes/menu.scn")
  32.        
  33.     var velocity = walk
  34.     if Input.is_action_pressed("run_mod"):
  35.         velocity = run
  36.    
  37.     motion = motion.normalized() * velocity * delta
  38.    
  39.     move(motion)
  40.    
  41.     timelapse += delta
  42.    
  43.     if get_node("RayCast2D").is_colliding() and Input.is_action_pressed("action"):
  44.         var collider = get_node("RayCast2D").get_collider()
  45.         send_to_dialog()
  46.    
  47.     # animation changes
  48.     if (old_motion == motion):
  49.         return
  50.        
  51.     old_motion = motion
  52.     if(motion == Vector2(0,0)):
  53.         anim += ("_idle")
  54.     elif(motion.x < 0):
  55.         anim = "left"
  56.         get_node("RayCast2D").set_rot(3*PI/2)
  57.     elif(motion.x > 0):
  58.         anim = "right"
  59.         get_node("RayCast2D").set_rot((PI)/2)
  60.     elif(motion.y < 0):
  61.         anim = "up"
  62.         get_node("RayCast2D").set_rot(PI)
  63.     elif(motion.y > 0):
  64.         anim = "down"
  65.         get_node("RayCast2D").set_rot(0)
  66.        
  67.     get_node("walk-anim").play(anim)
  68.    
  69.  
  70.  
  71.  
  72. func _ready():
  73.     set_fixed_process(true)
  74.     get_node("RayCast2D").add_exception(self)
  75.    
  76. func _fixed_process(delta):
  77.     process_movement(delta)
  78.    
  79. func send_to_dialog():
  80.     var dialog_send = get_tree().get_root().get_node("dialog-box")  # Attempt to call function 'recieve_text' in base 'null instance' on a null instance.
  81.    
  82.     dialog_send.receive_text(str(collider)) # naturally does not execute see above comment
  83.    
  84.     print (collider, str(timelapse))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement