Parcourir la source

Bouge avec le clavier

DricomDragon il y a 11 mois
Parent
commit
14882dd898
2 fichiers modifiés avec 40 ajouts et 1 suppressions
  1. 25 1
      godot/composants/objets/raquette/raquette.gd
  2. 15 0
      godot/project.godot

+ 25 - 1
godot/composants/objets/raquette/raquette.gd

@@ -2,9 +2,24 @@ class_name Raquette
 extends StaticBody2D
 
 
+const VITESSE_MAX : float = 20.0
+
+var _vitesse : float = 0.0
+
+
+func _physics_process(delta: float) -> void:
+	if not est_vivant():
+		return
+	position.x += _vitesse
+
+
 func _unhandled_input(evenement: InputEvent) -> void:
-	if evenement is InputEventMouseMotion and est_vivant():
+	if not est_vivant():
+		return
+	if evenement is InputEventMouseMotion:
 		_bouger_avec_souris(evenement)
+	elif evenement is InputEventKey:
+		_bouger_avec_clavier(evenement)
 
 
 func est_vivant() -> bool:
@@ -13,3 +28,12 @@ func est_vivant() -> bool:
 
 func _bouger_avec_souris(evenement_souris: InputEventMouseMotion) -> void:
 	position.x = evenement_souris.get_position().x
+
+
+func _bouger_avec_clavier(evenement_clavier: InputEventKey) -> void:
+	if evenement_clavier.is_action_pressed("bouger_a_droite"):
+		_vitesse = VITESSE_MAX
+	elif evenement_clavier.is_action_pressed("bouger_a_gauche"):
+		_vitesse = -VITESSE_MAX
+	if evenement_clavier.is_released() and (evenement_clavier.is_action("bouger_a_gauche") or evenement_clavier.is_action("bouger_a_droite")) :
+		_vitesse = 0.0

+ 15 - 0
godot/project.godot

@@ -20,6 +20,21 @@ config/icon="res://icon.svg"
 
 window/size/resizable=false
 
+[input]
+
+bouger_a_droite={
+"deadzone": 0.5,
+"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null)
+, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":101,"echo":false,"script":null)
+]
+}
+bouger_a_gauche={
+"deadzone": 0.5,
+"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"echo":false,"script":null)
+, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null)
+]
+}
+
 [rendering]
 
 renderer/rendering_method="gl_compatibility"