Fixed placeholder texture. Added correct ship movement
This commit is contained in:
+15
-6
@@ -1,10 +1,19 @@
|
||||
extends Node2D
|
||||
extends Node
|
||||
|
||||
|
||||
var position : Vector2:
|
||||
set(value):
|
||||
$Ship.position = value
|
||||
get:
|
||||
return $Ship.position
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
var speed := 100
|
||||
var input_direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
||||
var velocity := input_direction * speed
|
||||
position += velocity * delta
|
||||
var screen_size := get_viewport_rect().size
|
||||
position = position.clamp(Vector2.ZERO, screen_size)
|
||||
print(input_direction)
|
||||
|
||||
if input_direction.is_zero_approx():
|
||||
$Ship.decelerate($Ship.deceleration * delta)
|
||||
else:
|
||||
var acceleration : Vector2 = input_direction * $Ship.acceleration * delta
|
||||
$Ship.accelerate(acceleration)
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://xpj7f3l1l51l"]
|
||||
[gd_scene load_steps=3 format=3 uid="uid://bkv8y6n7chu3f"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://c2uf62j1im13p" path="res://game/entities/player.gd" id="1_xkeht"]
|
||||
[ext_resource type="PackedScene" uid="uid://dn44qgg4coedd" path="res://game/entities/ship.tscn" id="2_3a8sv"]
|
||||
[ext_resource type="Script" uid="uid://c2uf62j1im13p" path="res://game/entities/player.gd" id="1_3a8sv"]
|
||||
[ext_resource type="PackedScene" uid="uid://jvyagshykmgb" path="res://game/entities/ship.tscn" id="2_ktqbp"]
|
||||
|
||||
[node name="Player" type="Node2D"]
|
||||
script = ExtResource("1_xkeht")
|
||||
[node name="Player" type="Node"]
|
||||
script = ExtResource("1_3a8sv")
|
||||
|
||||
[node name="Ship" parent="." instance=ExtResource("2_3a8sv")]
|
||||
[node name="Ship" parent="." instance=ExtResource("2_ktqbp")]
|
||||
size = Vector2(48, 32)
|
||||
acceleration = 46
|
||||
deceleration = 23
|
||||
max_speed = 46
|
||||
|
||||
+36
-1
@@ -1,9 +1,44 @@
|
||||
extends Node2D
|
||||
|
||||
@export var size : Vector2
|
||||
@export var size : Vector2:
|
||||
set(value):
|
||||
size = value
|
||||
if $Sprite2D.texture:
|
||||
$Sprite2D.texture.size = value
|
||||
get:
|
||||
return size
|
||||
|
||||
@export var acceleration : int
|
||||
@export var deceleration : int
|
||||
@export var max_speed : int
|
||||
|
||||
var _velocity : Vector2
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
var texture := PlaceholderTexture2D.new()
|
||||
texture.size = size
|
||||
$Sprite2D.texture = texture
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
position += _velocity * delta
|
||||
|
||||
|
||||
func accelerate(value: Vector2) -> void:
|
||||
_velocity += value
|
||||
_velocity = _velocity.clamp(Vector2(-max_speed, -max_speed), Vector2(max_speed, max_speed))
|
||||
|
||||
|
||||
func decelerate(value: float) -> void:
|
||||
var current_speed := _velocity.length()
|
||||
|
||||
if current_speed <= 0:
|
||||
_velocity = Vector2.ZERO
|
||||
return
|
||||
|
||||
var new_speed := current_speed - value
|
||||
if new_speed < 0:
|
||||
new_speed = 0
|
||||
|
||||
_velocity = _velocity.normalized() * new_speed
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://dn44qgg4coedd"]
|
||||
[gd_scene load_steps=2 format=3 uid="uid://jvyagshykmgb"]
|
||||
|
||||
[sub_resource type="GDScript" id="GDScript_3a8sv"]
|
||||
script/source = "class_name Ship
|
||||
|
||||
extends Node2D
|
||||
"
|
||||
[ext_resource type="Script" uid="uid://cesibaqtrgotl" path="res://game/entities/ship.gd" id="1_6isjb"]
|
||||
|
||||
[node name="Ship" type="Node2D"]
|
||||
script = SubResource("GDScript_3a8sv")
|
||||
script = ExtResource("1_6isjb")
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ extends Node
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
var player : Node2D = load("res://game/entities/player.tscn").instantiate()
|
||||
var player : Node = load("res://game/entities/player.tscn").instantiate()
|
||||
player.position = Vector2(100, 100)
|
||||
add_child(player)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user