game1: add terrain collision detection

This commit is contained in:
Alice Gaudon 2020-08-18 18:49:16 +02:00
parent 862907f15b
commit b0762ec3db
2 changed files with 40 additions and 12 deletions

View File

@ -1,9 +1,27 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=3 format=2]
[ext_resource path="res://images/game1/game1_player.tres" type="SpriteFrames" id=1]
[sub_resource type="RectangleShape2D" id=1]
[node name="Player" type="AnimatedSprite"]
frames = ExtResource( 1 )
offset = Vector2( 8, 0 )
[node name="Camera2D" type="Camera2D" parent="."]
[node name="Sensors" type="Node2D" parent="."]
[node name="Right" type="Area2D" parent="Sensors"]
position = Vector2( 16, 0 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="Sensors/Right"]
scale = Vector2( 0.5, 0.5 )
shape = SubResource( 1 )
[node name="Left" type="Area2D" parent="Sensors"]
position = Vector2( -16, 0 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="Sensors/Left"]
scale = Vector2( 0.5, 0.5 )
shape = SubResource( 1 )

View File

@ -82,8 +82,6 @@ func _input(event):
go_left = true
if event.is_action_pressed('interact'):
do_interact = true
move()
interact()
if event.is_action_released('right'):
go_right = false
@ -92,12 +90,26 @@ func _input(event):
if event.is_action_released('interact'):
do_interact = false
var trying_to_move = false
func move():
if playing && !is_camera_moving && !is_interacting:
if go_right && !go_left:
move_right()
elif go_left && !go_right:
move_left()
if !playing || trying_to_move || is_interacting || is_camera_moving: return
trying_to_move = true
yield(get_tree(), "physics_frame")
yield(get_tree(), "physics_frame")
if go_right && !go_left && can_move_towards('Right'):
move_right()
elif go_left && !go_right && can_move_towards('Left'):
move_left()
trying_to_move = false
func _process(delta):
interact()
move()
func can_move_towards(sensorName : String) -> bool:
var sensor = player.get_node("Sensors/" + sensorName) as Area2D
return sensor.get_overlapping_bodies().size() == 0 && sensor.get_overlapping_areas().size() == 0
func move_right():
is_camera_moving = true
@ -110,7 +122,6 @@ func move_right():
player.frame = 0
offset_player(16)
is_camera_moving = false
move()
func move_left():
@ -124,7 +135,6 @@ func move_left():
player.play('default')
player.frame = 0
is_camera_moving = false
move()
func offset_player(offset):
@ -134,7 +144,7 @@ func offset_player(offset):
elif r == 2: player.position.x -= offset
elif r == 3: player.position.y -= offset
print(player.position / 16)
print((player.position - Vector2(8, 8)) / 16)
const animation_steps = [3, 6, 10, 13]
var is_camera_moving = false
@ -146,7 +156,7 @@ func update_camera():
var is_interacting = false
func interact():
if !playing || is_camera_moving || is_interacting || !do_interact: return
if !playing || !do_interact || is_interacting || trying_to_move || is_camera_moving: return
is_interacting = true
var level = levels[current_level]