Added world collision

This commit is contained in:
2025-10-21 16:20:28 +03:00
parent 19be23c539
commit 2d58e385fd
6 changed files with 59 additions and 18 deletions
+1
View File
@@ -7,6 +7,7 @@
script = ExtResource("1_3a8sv") script = ExtResource("1_3a8sv")
[node name="Ship" parent="." instance=ExtResource("2_ktqbp")] [node name="Ship" parent="." instance=ExtResource("2_ktqbp")]
motion_mode = 1
size = Vector2(48, 32) size = Vector2(48, 32)
acceleration = 46 acceleration = 46
deceleration = 23 deceleration = 23
+16 -13
View File
@@ -1,10 +1,13 @@
extends Node2D extends CharacterBody2D
@export var size : Vector2: @export var size : Vector2:
set(value): set(value):
size = value size = value
if $Sprite2D.texture: if $Sprite2D.texture:
$Sprite2D.texture.size = value $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: get:
return size return size
@@ -12,7 +15,7 @@ extends Node2D
@export var deceleration : int @export var deceleration : int
@export var max_speed : int @export var max_speed : int
var _velocity : Vector2 #var _velocity : Vector2
func _ready() -> void: func _ready() -> void:
@@ -21,29 +24,29 @@ func _ready() -> void:
$Sprite2D.texture = texture $Sprite2D.texture = texture
func _process(delta: float) -> void: func _physics_process(_delta: float) -> void:
position += _velocity * delta 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: func accelerate(direction: Vector2, delta: float) -> void:
var accel : Vector2 = direction * acceleration * delta var accel : Vector2 = direction * acceleration * delta
var decel : float = deceleration * delta var decel : float = deceleration * delta
_velocity.x = _get_new_speed(accel.x, decel, _velocity.x) velocity.x = _get_new_speed(accel.x, decel, velocity.x)
_velocity.y = _get_new_speed(accel.y, decel, _velocity.y) velocity.y = _get_new_speed(accel.y, decel, velocity.y)
if _velocity.length() > max_speed: if velocity.length() > max_speed:
_velocity = _velocity.normalized() * max_speed velocity = velocity.normalized() * max_speed
func _get_new_speed(accel: float, decel: float, current_speed: float) -> float: func _get_new_speed(accel: float, decel: float, current_speed: float) -> float:
if is_zero_approx(accel): if is_zero_approx(accel):
if absf(current_speed) < decel: if absf(current_speed) < decel:
return 0.0 return 0.0
else: return current_speed + (decel if current_speed < 0 else -decel)
if current_speed < 0:
return current_speed + decel
else:
return current_speed - decel
else: else:
return current_speed + accel return current_speed + accel
+11 -2
View File
@@ -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"] [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") script = ExtResource("1_6isjb")
[node name="Sprite2D" type="Sprite2D" parent="."] [node name="Sprite2D" type="Sprite2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CapsuleShape2D_6isjb")
-1
View File
@@ -5,4 +5,3 @@ func _ready() -> void:
var player : Node = load("res://game/entities/player.tscn").instantiate() var player : Node = load("res://game/entities/player.tscn").instantiate()
player.position = Vector2(100, 100) player.position = Vector2(100, 100)
add_child(player) add_child(player)
+27 -2
View File
@@ -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"] [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") 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
+4
View File
@@ -344,6 +344,10 @@ previous_reactor_scheme={
] ]
} }
[layer_names]
2d_physics/layer_1="Player"
[rendering] [rendering]
textures/canvas_textures/default_texture_filter=0 textures/canvas_textures/default_texture_filter=0