diff --git a/game/entities/player.tscn b/game/entities/player.tscn index babbfc9..2e8d4c6 100644 --- a/game/entities/player.tscn +++ b/game/entities/player.tscn @@ -7,6 +7,7 @@ script = ExtResource("1_3a8sv") [node name="Ship" parent="." instance=ExtResource("2_ktqbp")] +motion_mode = 1 size = Vector2(48, 32) acceleration = 46 deceleration = 23 diff --git a/game/entities/ship.gd b/game/entities/ship.gd index 8f07f99..dd2ec19 100644 --- a/game/entities/ship.gd +++ b/game/entities/ship.gd @@ -1,10 +1,13 @@ -extends Node2D +extends CharacterBody2D @export var size : Vector2: set(value): size = value if $Sprite2D.texture: $Sprite2D.texture.size = value + $CollisionShape2D.shape.radius = 0.9 * minf(size.x, size.y)/2 + $CollisionShape2D.shape.height = 0.9 * maxf(size.x, size.y) + $CollisionShape2D.rotation = 0.0 if size.x < size.y else PI/2 get: return size @@ -12,7 +15,7 @@ extends Node2D @export var deceleration : int @export var max_speed : int -var _velocity : Vector2 +#var _velocity : Vector2 func _ready() -> void: @@ -21,29 +24,29 @@ func _ready() -> void: $Sprite2D.texture = texture -func _process(delta: float) -> void: - position += _velocity * delta +func _physics_process(_delta: float) -> void: + var was_collided := move_and_slide() + if was_collided: + var normal := get_wall_normal() + velocity -= normal.abs() * velocity + print(was_collided, get_wall_normal(), velocity) func accelerate(direction: Vector2, delta: float) -> void: var accel : Vector2 = direction * acceleration * delta var decel : float = deceleration * delta - _velocity.x = _get_new_speed(accel.x, decel, _velocity.x) - _velocity.y = _get_new_speed(accel.y, decel, _velocity.y) + velocity.x = _get_new_speed(accel.x, decel, velocity.x) + velocity.y = _get_new_speed(accel.y, decel, velocity.y) - if _velocity.length() > max_speed: - _velocity = _velocity.normalized() * max_speed + if velocity.length() > max_speed: + velocity = velocity.normalized() * max_speed func _get_new_speed(accel: float, decel: float, current_speed: float) -> float: if is_zero_approx(accel): if absf(current_speed) < decel: return 0.0 - else: - if current_speed < 0: - return current_speed + decel - else: - return current_speed - decel + return current_speed + (decel if current_speed < 0 else -decel) else: return current_speed + accel diff --git a/game/entities/ship.tscn b/game/entities/ship.tscn index 4416c72..a665a63 100644 --- a/game/entities/ship.tscn +++ b/game/entities/ship.tscn @@ -1,8 +1,17 @@ -[gd_scene load_steps=2 format=3 uid="uid://jvyagshykmgb"] +[gd_scene load_steps=3 format=3 uid="uid://jvyagshykmgb"] [ext_resource type="Script" uid="uid://cesibaqtrgotl" path="res://game/entities/ship.gd" id="1_6isjb"] -[node name="Ship" type="Node2D"] +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_6isjb"] +height = 20.0 + +[node name="Ship" type="CharacterBody2D"] +disable_mode = 1 +motion_mode = 1 +wall_min_slide_angle = 3.1415927 script = ExtResource("1_6isjb") [node name="Sprite2D" type="Sprite2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CapsuleShape2D_6isjb") diff --git a/game/passage.gd b/game/passage.gd index ce581b2..a189333 100644 --- a/game/passage.gd +++ b/game/passage.gd @@ -5,4 +5,3 @@ func _ready() -> void: var player : Node = load("res://game/entities/player.tscn").instantiate() player.position = Vector2(100, 100) add_child(player) - diff --git a/game/passage.tscn b/game/passage.tscn index 7ba7a50..56ffa21 100644 --- a/game/passage.tscn +++ b/game/passage.tscn @@ -1,6 +1,31 @@ -[gd_scene load_steps=2 format=3 uid="uid://dgc0087kvarx6"] +[gd_scene load_steps=3 format=3 uid="uid://dgc0087kvarx6"] [ext_resource type="Script" uid="uid://c6gpm3edyr4nu" path="res://game/passage.gd" id="1_ltkyg"] -[node name="Passage" type="Node"] +[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_ltkyg"] + +[node name="Passage" type="StaticBody2D"] script = ExtResource("1_ltkyg") + +[node name="CollisionTop" type="CollisionShape2D" parent="."] +position = Vector2(320, 0) +rotation = 3.1415927 +shape = SubResource("WorldBoundaryShape2D_ltkyg") +one_way_collision = true + +[node name="CollisionBottom" type="CollisionShape2D" parent="."] +position = Vector2(320, 360) +shape = SubResource("WorldBoundaryShape2D_ltkyg") +one_way_collision = true + +[node name="CollisionLeft" type="CollisionShape2D" parent="."] +position = Vector2(0, 180) +rotation = 1.5707964 +shape = SubResource("WorldBoundaryShape2D_ltkyg") +one_way_collision = true + +[node name="CollisionRight" type="CollisionShape2D" parent="."] +position = Vector2(640, 180) +rotation = -1.5707964 +shape = SubResource("WorldBoundaryShape2D_ltkyg") +one_way_collision = true diff --git a/project.godot b/project.godot index 0f24bc7..4a149f7 100644 --- a/project.godot +++ b/project.godot @@ -344,6 +344,10 @@ previous_reactor_scheme={ ] } +[layer_names] + +2d_physics/layer_1="Player" + [rendering] textures/canvas_textures/default_texture_filter=0