parent
9b79c05a7b
commit
8152546bf2
@ -0,0 +1,65 @@ |
||||
extends CharacterBody2D |
||||
|
||||
|
||||
const SPEED = 300.0 |
||||
const JUMP_VELOCITY = -400.0 |
||||
|
||||
const STATES= [ |
||||
"spawn", |
||||
"idle", |
||||
"walk", |
||||
"jump", |
||||
"die" |
||||
] |
||||
|
||||
# Get the gravity from the project settings to be synced with RigidBody nodes. |
||||
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") |
||||
|
||||
func _ready(): |
||||
$fsm.set_states(STATES) |
||||
|
||||
func _enter_spawn(): |
||||
$fsm.set_next_state("idle") |
||||
|
||||
func on_enter_idle_state(): |
||||
velocity.x = 0.0 |
||||
|
||||
func idle_state(_delta): |
||||
var direction = Input.get_axis("ui_left", "ui_right") |
||||
|
||||
if direction: |
||||
$fsm.set_next_state("walk") |
||||
elif Input.is_action_just_pressed("ui_accept") and is_on_floor(): |
||||
$fsm.set_next_state("jump") |
||||
|
||||
func walk_state(_delta): |
||||
var direction = Input.get_axis("ui_left", "ui_right") |
||||
|
||||
if direction: |
||||
velocity.x = direction * SPEED |
||||
else: |
||||
$fsm.set_next_state("idle") |
||||
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor(): |
||||
$fsm.set_next_state("jump") |
||||
|
||||
func on_enter_jump_state(): |
||||
velocity.y = JUMP_VELOCITY |
||||
|
||||
func jump_state(_delta): |
||||
var direction = Input.get_axis("ui_left", "ui_right") |
||||
|
||||
velocity.x = move_toward(velocity.x, direction * SPEED, SPEED) |
||||
|
||||
if is_on_floor(): |
||||
if direction: |
||||
$fsm.set_next_state("walk") |
||||
else: |
||||
$fsm.set_next_state("idle") |
||||
|
||||
func after_state(delta): |
||||
if $fsm.current_state in ["idle", "walk"]: |
||||
move_and_slide() |
||||
|
||||
if not is_on_floor(): |
||||
velocity.y += gravity * delta |
||||
@ -0,0 +1,27 @@ |
||||
[gd_scene load_steps=5 format=3 uid="uid://c5rmt3jeffjx7"] |
||||
|
||||
[ext_resource type="Script" path="res://nodes/player/player.gd" id="1_exx2i"] |
||||
[ext_resource type="Texture2D" uid="uid://cm4rn4vgyjd1b" path="res://nodes/player/temp.png" id="1_s33dc"] |
||||
[ext_resource type="Script" path="res://scripts/ia/fsm.gd" id="2_ix1o4"] |
||||
|
||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_w17ly"] |
||||
radius = 7.9984 |
||||
height = 31.9936 |
||||
|
||||
[node name="player" type="CharacterBody2D"] |
||||
script = ExtResource("1_exx2i") |
||||
|
||||
[node name="fsm" type="Node2D" parent="." node_paths=PackedStringArray("root")] |
||||
script = ExtResource("2_ix1o4") |
||||
root = NodePath("..") |
||||
|
||||
[node name="sprite" type="Sprite2D" parent="."] |
||||
texture = ExtResource("1_s33dc") |
||||
|
||||
[node name="shape" type="CollisionShape2D" parent="."] |
||||
rotation = 1.55081 |
||||
shape = SubResource("CapsuleShape2D_w17ly") |
||||
metadata/_edit_group_ = true |
||||
|
||||
[node name="camera" type="Camera2D" parent="."] |
||||
zoom = Vector2(2, 2) |
||||
|
After Width: | Height: | Size: 1.0 KiB |
@ -0,0 +1,34 @@ |
||||
[remap] |
||||
|
||||
importer="texture" |
||||
type="CompressedTexture2D" |
||||
uid="uid://cm4rn4vgyjd1b" |
||||
path="res://.godot/imported/temp.png-1af0064fad142262002520a17592c851.ctex" |
||||
metadata={ |
||||
"vram_texture": false |
||||
} |
||||
|
||||
[deps] |
||||
|
||||
source_file="res://nodes/player/temp.png" |
||||
dest_files=["res://.godot/imported/temp.png-1af0064fad142262002520a17592c851.ctex"] |
||||
|
||||
[params] |
||||
|
||||
compress/mode=0 |
||||
compress/high_quality=false |
||||
compress/lossy_quality=0.7 |
||||
compress/hdr_compression=1 |
||||
compress/normal_map=0 |
||||
compress/channel_pack=0 |
||||
mipmaps/generate=false |
||||
mipmaps/limit=-1 |
||||
roughness/mode=0 |
||||
roughness/src_normal="" |
||||
process/fix_alpha_border=true |
||||
process/premult_alpha=false |
||||
process/normal_map_invert_y=false |
||||
process/hdr_as_srgb=false |
||||
process/hdr_clamp_exposure=false |
||||
process/size_limit=0 |
||||
detect_3d/compress_to=1 |
||||
File diff suppressed because one or more lines are too long
@ -0,0 +1,49 @@ |
||||
extends Node2D |
||||
|
||||
class_name FSM |
||||
|
||||
@export var root : Node |
||||
|
||||
var states = ['idle'] |
||||
var current_state = null |
||||
var previous_state = null |
||||
var next_state = null |
||||
|
||||
func set_states(new_states): |
||||
self.states = new_states |
||||
|
||||
func _ready(): |
||||
if current_state == null: |
||||
next_state = states[0] |
||||
|
||||
_transition() |
||||
|
||||
func _physics_process(delta): |
||||
if "before_state" in root: |
||||
root.call("before_state", delta) |
||||
|
||||
var current_state_method = current_state + "_state" |
||||
if current_state_method in root: |
||||
root.call(current_state_method, delta) |
||||
|
||||
if "after_state" in root: |
||||
root.call("after_state", delta) |
||||
|
||||
_transition() |
||||
|
||||
func _transition(): |
||||
if next_state != null: |
||||
previous_state = current_state |
||||
current_state = next_state |
||||
next_state = null |
||||
|
||||
var on_exit_state_method = "on_exit_" + current_state + "_state" |
||||
if on_exit_state_method in root: |
||||
root.call(on_exit_state_method) |
||||
|
||||
var on_enter_state_method = "on_enter_" + current_state + "_state" |
||||
if on_enter_state_method in root: |
||||
root.call(on_enter_state_method) |
||||
|
||||
func set_next_state(state): |
||||
next_state = state |
||||
Loading…
Reference in new issue