Browse Source

:tada: Make the chicken run if touched by a wheel

Become angry first, then run.
DricomDragon 2 years ago
parent
commit
ef095175f1

+ 39 - 3
godot/component/entity/chick/Chicken.gd

@@ -2,11 +2,16 @@ class_name Chicken
 extends KinematicBody2D
 
 
+enum State {IDLE, WALKING, ANGRY, RUNNING}
+
 export var walk_linear_speed = 100
 export var run_boost = 4
+export var push_impulse = 100
 
 var velocity = Vector2.ZERO
 
+var _current_state = State.IDLE
+
 
 func attack(from: Vector2, angle: float):
 	position = from
@@ -15,9 +20,11 @@ func attack(from: Vector2, angle: float):
 	$AnimatedSprite.play("walk")
 	_update_sprite_direction()
 
+	_current_state = State.WALKING
+
 
 func _physics_process(delta):
-	var collision = move_and_collide(velocity * delta)
+	var collision = move_and_collide(velocity * delta, false)
 	_collide_and_react(collision)
 
 
@@ -29,6 +36,11 @@ func _on_Spawner_reset():
 	_die()
 
 
+func _on_AngryTimer_timeout():
+	assert(_current_state == State.ANGRY)
+	_start_running()
+
+
 func _die():
 	queue_free()
 
@@ -38,8 +50,16 @@ func _collide_and_react(collision: KinematicCollision2D) -> void:
 		return
 
 	var collider = collision.get_collider()
-	if collider is KinematicBody2D and (collider as Node).is_in_group("chicken") :
-		_turn_away()
+	if collider is Node2D:
+		var node_collider = collider as Node
+		if _current_state == State.WALKING:
+			if node_collider.is_in_group("chicken"):
+				_turn_away()
+			elif node_collider.is_in_group("wheel"):
+				_get_angry()
+
+	if collider is RigidBody2D:
+		_push_away(collider as RigidBody2D, -collision.normal)
 
 
 func _turn_away() -> void:
@@ -47,5 +67,21 @@ func _turn_away() -> void:
 	_update_sprite_direction()
 
 
+func _push_away(body: RigidBody2D, dir: Vector2) -> void:
+	body.apply_central_impulse(dir * push_impulse)
+
+
+func _get_angry() -> void:
+	$AngryTimer.start()
+	$AnimatedSprite.play("get_hit")
+	_current_state = State.ANGRY
+
+
+func _start_running() -> void:
+	velocity *= run_boost
+	$AnimatedSprite.play("run")
+	_current_state = State.RUNNING
+
+
 func _update_sprite_direction() -> void:
 	$AnimatedSprite.flip_h = velocity.x < 0.0

+ 8 - 0
godot/component/entity/chick/Chicken.tscn

@@ -20,4 +20,12 @@ shape = SubResource( 1 )
 [node name="VisibilityNotifier2D" type="VisibilityNotifier2D" parent="."]
 rect = Rect2( -40, -40, 80, 80 )
 
+[node name="AngryTimer" type="Timer" parent="."]
+process_mode = 0
+one_shot = true
+__meta__ = {
+"_editor_description_": "The time the chicken will stop when hit before it starts running."
+}
+
 [connection signal="screen_exited" from="VisibilityNotifier2D" to="." method="_on_VisibilityNotifier2D_screen_exited"]
+[connection signal="timeout" from="AngryTimer" to="." method="_on_AngryTimer_timeout"]

+ 1 - 1
godot/component/entity/wheel/Wheel.tscn

@@ -10,7 +10,7 @@ bounce = 0.9
 [sub_resource type="CircleShape2D" id=1]
 radius = 33.1812
 
-[node name="Wheel" type="RigidBody2D"]
+[node name="Wheel" type="RigidBody2D" groups=["wheel"]]
 mass = 0.25
 physics_material_override = SubResource( 2 )
 angular_damp = 0.0