player and level

This commit is contained in:
Patrick Nueser
2024-06-27 10:53:06 +02:00
parent 27b9bb4777
commit 5a321ed45b
6 changed files with 226 additions and 1 deletions

57
player/player.gd Normal file
View File

@ -0,0 +1,57 @@
extends Area2D
class_name Player
@onready var sprite_2d = $Sprite2D
@onready var animation_player = $AnimationPlayer
@export var speed: float = 250.0
const MARGIN: float = 32.0
var _upper_left: Vector2
var _lower_right: Vector2
# Called when the node enters the scene tree for the first time.
func _ready():
var vp = get_viewport_rect()
_lower_right = Vector2(
vp.size.x - MARGIN,
vp.size.y - MARGIN
)
_upper_left = Vector2(MARGIN, MARGIN)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var input = get_input()
global_position += input * delta * speed
global_position = global_position.clamp(
_upper_left,
_lower_right
)
func get_input() -> Vector2:
var v = Vector2(
Input.get_axis("left", "right"),
Input.get_axis("up", "down")
)
if v.x != 0:
animation_player.play("turn")
if v.x > 0:
sprite_2d.flip_h = true
else:
sprite_2d.flip_h = false
else:
animation_player.play("fly")
return v.normalized()

78
player/player.tscn Normal file
View File

@ -0,0 +1,78 @@
[gd_scene load_steps=8 format=3 uid="uid://c6h48t3c4xja"]
[ext_resource type="Texture2D" uid="uid://dblif3743kgka" path="res://assets/ships/ships_human_1.png" id="1_2i5ry"]
[ext_resource type="Script" path="res://player/player.gd" id="1_sh380"]
[sub_resource type="CircleShape2D" id="CircleShape2D_3fimp"]
radius = 14.0
[sub_resource type="Animation" id="Animation_qjn2v"]
resource_name = "fly"
length = 0.2
loop_mode = 1
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:frame")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.1),
"transitions": PackedFloat32Array(1, 1),
"update": 1,
"values": [0, 1]
}
[sub_resource type="Animation" id="Animation_w83h7"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:frame")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [0]
}
[sub_resource type="Animation" id="Animation_bctg6"]
resource_name = "turn"
length = 0.2
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:frame")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.1),
"transitions": PackedFloat32Array(1, 1),
"update": 1,
"values": [2, 3]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_6dw4b"]
_data = {
"RESET": SubResource("Animation_w83h7"),
"fly": SubResource("Animation_qjn2v"),
"turn": SubResource("Animation_bctg6")
}
[node name="Player" type="Area2D"]
script = ExtResource("1_sh380")
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("1_2i5ry")
hframes = 4
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_3fimp")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_6dw4b")
}
autoplay = "fly"