among-eux

Game made in 1 day with flx
git clone git://git.vgx.fr/among-eux
Log | Files | Refs

Guard.gd (1548B)


      1 extends KinematicBody2D
      2 
      3 
      4 # Declare member variables here. Examples:
      5 # var a = 2
      6 # var b = "text"
      7 
      8 export var stop_dist = 10.0
      9 export var speed = 70.0
     10 export var dash_speed = 200.0
     11 export var dash_timeout = 3.0
     12 export var dash_duration = .3
     13 
     14 var target = Vector2()
     15 var dash_state = -dash_timeout
     16 
     17 var last_click = 0.0
     18 
     19 # Called when the node enters the scene tree for the first time.
     20 func _ready():
     21 	target = position
     22 
     23 func _unhandled_input(event):
     24 	if event is InputEventMouseButton and event.pressed:
     25 		var date = Time.get_ticks_msec()
     26 		target = event.position
     27 		if (event.doubleclick or date-last_click < 250.0 ) and dash_state <= -dash_timeout:
     28 			dash_state = dash_duration
     29 			$Dash.visible = true
     30 		last_click = date
     31 
     32 func _physics_process(delta):
     33 	var to_target = target - position
     34 	if to_target.length() > stop_dist:
     35 		var move_speed = dash_speed if dash_state > 0 else speed
     36 		move_and_slide(to_target.normalized()*move_speed)
     37 		
     38 		var direction = int(rad2deg(to_target.angle())+360+45)%360/90
     39 		if direction == 0:
     40 			$Sprite.animation = "right"
     41 		elif direction == 1:
     42 			$Sprite.animation = "down"
     43 		elif direction == 2:
     44 			$Sprite.animation = "left"
     45 		elif direction == 3:
     46 			$Sprite.animation = "up"
     47 		
     48 		if dash_state >= 0:
     49 			$Sprite.animation = "dash_" + $Sprite.animation
     50 		
     51 		$Sprite.playing = true
     52 	else:
     53 		$Sprite.playing = false
     54 		$Sprite.frame = 0
     55 	
     56 	if dash_state > -dash_timeout:
     57 		dash_state -= delta
     58 		if dash_state <= 0:
     59 			$Dash.scale.x = (dash_timeout+dash_state)/dash_timeout * 0.01
     60 
     61 	else:
     62 		$Dash.visible = false
     63