diff --git a/nodes/player/player.gd b/nodes/player/player.gd new file mode 100644 index 0000000..c11c875 --- /dev/null +++ b/nodes/player/player.gd @@ -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 diff --git a/nodes/player/player.tscn b/nodes/player/player.tscn new file mode 100644 index 0000000..7592a3e --- /dev/null +++ b/nodes/player/player.tscn @@ -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) diff --git a/nodes/player/temp.png b/nodes/player/temp.png new file mode 100644 index 0000000..1302e23 Binary files /dev/null and b/nodes/player/temp.png differ diff --git a/nodes/player/temp.png.import b/nodes/player/temp.png.import new file mode 100644 index 0000000..de50c32 --- /dev/null +++ b/nodes/player/temp.png.import @@ -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 diff --git a/scenes/poc.tscn b/scenes/poc.tscn index 013193d..fd8297e 100644 --- a/scenes/poc.tscn +++ b/scenes/poc.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=12 format=3 uid="uid://ce8ov6npvl27w"] +[gd_scene load_steps=13 format=3 uid="uid://ce8ov6npvl27w"] [ext_resource type="Texture2D" uid="uid://cfq8pma4bpgq" path="res://assets/tilesets/cave_tileset.png" id="1_vofrt"] +[ext_resource type="PackedScene" uid="uid://c5rmt3jeffjx7" path="res://nodes/player/player.tscn" id="2_gxv3v"] [sub_resource type="NavigationPolygon" id="NavigationPolygon_3t6yd"] vertices = PackedVector2Array(0, -16, 16, -16, 16, 16, 0, 16) @@ -216,3 +217,6 @@ sources/1 = SubResource("TileSetAtlasSource_adyds") tile_set = SubResource("TileSet_jhgsf") format = 2 layer_0/tile_data = PackedInt32Array(-1, 1, 3, 65535, 1, 4, -65536, 65537, 3, 0, 65537, 4, -65535, 131073, 3, 1, 131073, 4, -65534, 1, 3, 2, 1, 4, -65533, 65537, 3, 3, 65537, 4, -65532, 131073, 3, 4, 131073, 4, -65531, 1, 3, 5, 1, 4, -65530, 65537, 3, 6, 65537, 4, -65529, 131073, 3, 7, 131073, 4, -65528, 1, 3, 8, 1, 4, -65527, 65537, 3, 9, 65537, 4, -65526, 131073, 3, 10, 131073, 4, -65525, 1, 3, 11, 1, 4, -65524, 65537, 3, 12, 65537, 4, -65523, 131073, 3, 13, 131073, 4, -65522, 1, 3, 14, 1, 4, -65521, 65537, 3, 15, 65537, 4, -65520, 131073, 3, 16, 131073, 4, -65519, 1, 3, 17, 1, 4, -65518, 65537, 3, 18, 65537, 4, -65517, 131073, 3, 19, 131073, 4, -65516, 1, 3, 20, 1, 4, -65515, 65537, 3, 21, 65537, 4, -65514, 131073, 3, 22, 131073, 4, -65513, 1, 3, 23, 1, 4, -65512, 65537, 3, 24, 65537, 4, -65511, 131073, 3, 25, 131073, 4, -65510, 1, 3, 26, 1, 4, -65509, 65537, 3, 27, 65537, 4, -65508, 131073, 3, 28, 131073, 4, -65507, 1, 3, 29, 1, 4, -65506, 65537, 3, 30, 65537, 4, -65505, 131073, 3, 31, 131073, 4, -65504, 1, 3, 32, 1, 4, -65503, 65537, 3, 33, 65537, 4, -65502, 131073, 3, 34, 131073, 4, -65501, 1, 3, 35, 1, 4, -65500, 65537, 3, 36, 65537, 4, -65499, 131073, 3, 37, 131073, 4, -65498, 1, 3, 38, 1, 4, -65497, 65537, 3, 39, 65537, 4, -65496, 131073, 3, 40, 131073, 4, 655360, 65537, 6, 720896, 1, 4, 655361, 65537, 6, 720897, 65537, 4, 655362, 65537, 6, 720898, 131073, 4, 655363, 65537, 6, 720899, 1, 4, 655364, 65537, 6, 720900, 65537, 4, 655365, 65537, 6, 720901, 131073, 4, 655366, 65537, 6, 720902, 1, 4, 655367, 65537, 6, 720903, 65537, 4, 655368, 65537, 6, 720904, 131073, 4, 655369, 65537, 6, 720905, 1, 4, 655370, 65537, 6, 720906, 65537, 4, 655371, 65537, 6, 720907, 131073, 4, 655372, 65537, 6, 720908, 1, 4, 655373, 65537, 6, 720909, 65537, 4, 655374, 65537, 6, 720910, 131073, 4, 655375, 65537, 6, 720911, 1, 4, 655376, 65537, 6, 720912, 65537, 4, 655377, 65537, 6, 720913, 131073, 4, 655378, 65537, 4, 720914, 1, 4, 655379, 65537, 6, 720915, 65537, 4, 655380, 65537, 6, 720916, 131073, 4, 655381, 65537, 6, 720917, 1, 4, 655382, 65537, 6, 720918, 65537, 4, 655383, 65537, 6, 720919, 131073, 4, 655384, 65537, 6, 720920, 1, 4, 655385, 65537, 6, 720921, 65537, 4, 655386, 65537, 6, 720922, 131073, 4, 655387, 65537, 6, 720923, 1, 4, 655388, 65537, 6, 720924, 65537, 4, 655389, 65537, 6, 720925, 131073, 4, 655390, 65537, 6, 720926, 1, 4, 655391, 65537, 6, 720927, 65537, 4, 655392, 65537, 6, 720928, 131073, 4, 655393, 65537, 6, 720929, 1, 4, 655394, 65537, 6, 720930, 65537, 4, 655395, 65537, 6, 720931, 131073, 4, 655396, 65537, 6, 720932, 1, 4, 655397, 65537, 6, 720933, 65537, 4, 655398, 65537, 6, 720934, 131073, 4, 524293, 65537, 6, 589829, 1, 4, 524294, 65537, 6, 589830, 65537, 4, 524295, 65537, 6, 589831, 131073, 4, 524296, 65537, 6, 589832, 1, 4, 524297, 65537, 6, 589833, 65537, 4, 524298, 65537, 6, 589834, 131073, 4, 524299, 65537, 6, 589835, 1, 4, 524300, 65537, 4, 393229, 65537, 6, 458765, 1, 4, 393230, 65537, 6, 458766, 65537, 4, 393231, 65537, 6, 458767, 131073, 4, 393232, 65537, 6, 458768, 1, 4, 393233, 65537, 6, 458769, 65537, 4, 393234, 65537, 6, 458770, 131073, 4, 589843, 1, 4, 524308, 65537, 6, 589844, 65537, 4, 524309, 65537, 6, 589845, 131073, 4, 524310, 65537, 6, 589846, 1, 4, 524311, 65537, 6, 589847, 65537, 4, 524312, 65537, 4, 589848, 65537, 4, 524313, 65537, 6, 589849, 65537, 6, 524314, 65537, 6, 589850, 65537, 5, 524315, 65537, 6, 589851, 65537, 6, 262163, 65537, 6, 327699, 1, 4, 262164, 65537, 6, 327700, 65537, 4, 262165, 65537, 6, 327701, 131073, 4, 262166, 65537, 6, 327702, 1, 4, 262167, 65537, 6, 327703, 65537, 4, 262168, 65537, 6, 327704, 131073, 4, 262169, 65537, 6, 327705, 1, 4, 262170, 65537, 6, 327706, 65537, 4, 262171, 65537, 6, 327707, 131073, 4, 131088, 65537, 6, 196624, 1, 4, 131089, 65537, 6, 196625, 65537, 4, 131090, 65537, 6, 196626, 131073, 4, 131085, 65537, 6, 196621, 1, 4, 131086, 65537, 6, 196622, 65537, 4, 131087, 65537, 6, 196623, 131073, 4, 131082, 65537, 6, 196618, 1, 4, 131083, 65537, 6, 196619, 65537, 4, 131084, 65537, 6, 196620, 131073, 4, 262150, 65537, 6, 327686, 1, 4, 262151, 65537, 6, 327687, 65537, 4, 262152, 65537, 6, 327688, 131073, 4, 262147, 65537, 6, 327683, 1, 4, 262148, 65537, 6, 327684, 65537, 4, 262149, 65537, 6, 327685, 131073, 4, 262144, 65537, 6, 327680, 65537, 6, 262145, 65537, 6, 327681, 65537, 4, 262146, 65537, 6, 327682, 131073, 4, 196617, 65537, 6, 262153, 65537, 4, 458764, 65537, 6, 589842, 65537, 6, 393236, 65537, 4, 393237, 65537, 4, 393238, 65537, 4, 393239, 65537, 4, 458775, 65537, 4, 458774, 65537, 4, 458776, 65537, 4, 131071, 131073, 5, 327679, 131073, 5, 262143, 131073, 5, 196607, 131073, 5, 393215, 131073, 5, 458751, 131073, 5, 524287, 131073, 5, 589823, 131073, 5, 655359, 131073, 5, 720895, 131073, 5, 786431, 131073, 5, 41, 1, 5, 458793, 1, 5, 393257, 1, 5, 327721, 1, 5, 262185, 1, 5, 196649, 1, 5, 131113, 1, 5, 65577, 1, 5, 524329, 1, 5, 589865, 1, 5, 655401, 1, 5, 720937, 65537, 4, 720935, 65537, 4, 720936, 65537, 4, 655399, 65537, 6, 655400, 65537, 6, 589861, 65537, 6, 524325, 65537, 6, 458789, 65537, 6, 393253, 65537, 6, 327717, 65537, 6, 262181, 65537, 6, 196645, 65537, 6, 131109, 65537, 6, 65573, 65537, 6, 65572, 65537, 6, 65571, 65537, 6, 65570, 65537, 6, 65569, 65537, 6, 65568, 65537, 6, 65567, 65537, 6, 65566, 65537, 6, 65565, 65537, 6, 65564, 65537, 6, 65563, 65537, 6, 65562, 65537, 6, 65561, 65537, 6, 65560, 65537, 6, 65559, 65537, 6, 65558, 65537, 6, 65557, 65537, 6, 65556, 65537, 6, 65555, 65537, 6, 65554, 65537, 6, 65553, 65537, 6, 65552, 65537, 6, 65551, 65537, 6, 65550, 65537, 6, 65549, 65537, 6, 65548, 65537, 6, 65547, 65537, 6, 65546, 65537, 6, 65545, 65537, 6, 65544, 65537, 6, 65543, 65537, 6, 65542, 65537, 6, 65541, 65537, 6, 65540, 65537, 6, 65539, 65537, 6, 65538, 65537, 6, 65537, 65537, 6, 65536, 65537, 6, 131072, 65537, 6, 196608, 65537, 6, 196609, 65537, 6, 131073, 65537, 5, 131074, 65537, 6, 196610, 65537, 6, 196611, 65537, 6, 131075, 65537, 6, 131076, 65537, 5, 196612, 65537, 6, 196613, 65537, 6, 131077, 65537, 6, 131078, 65537, 6, 196614, 65537, 6, 196615, 65537, 6, 131079, 65537, 6, 131080, 65537, 6, 196616, 65537, 6, 131081, 65537, 6, 131091, 65537, 6, 196627, 65537, 6, 196628, 65537, 6, 131092, 65537, 6, 131093, 65537, 6, 196629, 65537, 6, 196630, 65537, 6, 131094, 65537, 5, 131095, 65537, 6, 196631, 65537, 6, 196632, 65537, 6, 131096, 65537, 6, 131097, 65537, 6, 196633, 65537, 6, 196634, 65537, 6, 131098, 65537, 6, 131099, 65537, 6, 196635, 65537, 6, 196636, 65537, 6, 131100, 65537, 6, 131101, 65537, 6, 196637, 65537, 5, 262173, 65537, 6, 262172, 65537, 6, 327708, 65537, 6, 393244, 65537, 6, 393243, 65537, 6, 393242, 65537, 6, 393241, 65537, 6, 393240, 65537, 6, 458777, 65537, 6, 458778, 65537, 6, 458779, 65537, 5, 458780, 65537, 6, 524316, 65537, 6, 589852, 65537, 6, 589853, 65537, 6, 524317, 65537, 6, 458781, 65537, 6, 393245, 65537, 6, 327709, 65537, 6, 327710, 65537, 6, 262174, 65537, 6, 196638, 65537, 6, 131102, 65537, 6, 131103, 65537, 6, 196639, 65537, 6, 262175, 65537, 6, 327711, 65537, 6, 393247, 65537, 6, 393246, 65537, 6, 458782, 65537, 6, 524318, 65537, 5, 589854, 65537, 5, 589855, 65537, 6, 524319, 65537, 6, 458783, 65537, 6, 458784, 65537, 6, 393248, 65537, 6, 327712, 65537, 6, 262176, 65537, 6, 196640, 65537, 6, 131104, 65537, 6, 131105, 65537, 6, 196641, 65537, 6, 262177, 65537, 6, 327713, 65537, 5, 393249, 65537, 6, 458785, 65537, 6, 524321, 65537, 6, 524320, 65537, 6, 589856, 65537, 6, 589857, 65537, 6, 589858, 65537, 6, 524322, 65537, 6, 458786, 65537, 6, 393250, 65537, 6, 327714, 65537, 6, 262178, 65537, 6, 196642, 65537, 6, 131106, 65537, 6, 131107, 65537, 6, 196643, 65537, 6, 262179, 65537, 6, 327715, 65537, 6, 393251, 65537, 6, 458787, 65537, 6, 524323, 65537, 6, 589859, 65537, 6, 589860, 65537, 6, 524324, 65537, 6, 458788, 65537, 6, 393252, 65537, 6, 327716, 65537, 6, 262180, 65537, 6, 196644, 65537, 6, 131108, 65537, 6, 65574, 65537, 6, 131110, 65537, 6, 196646, 65537, 6, 262182, 65537, 6, 327718, 65537, 6, 393254, 65537, 6, 458790, 65537, 6, 524326, 65537, 6, 589862, 65537, 6, 589863, 65537, 5, 524327, 65537, 6, 458791, 65537, 6, 393255, 65537, 6, 327719, 65537, 6, 262183, 65537, 6, 196647, 65537, 6, 131111, 65537, 6, 65575, 65537, 6, 65576, 65537, 6, 131112, 65537, 6, 196648, 65537, 6, 262184, 65537, 6, 327720, 65537, 6, 393256, 65537, 6, 458792, 65537, 6, 524328, 65537, 6, 589864, 65537, 6, 524307, 65537, 6, 458771, 65537, 6, 393235, 65537, 6, 458772, 65537, 6, 458773, 65537, 6, 524306, 65537, 6, 524305, 65537, 6, 524304, 65537, 6, 524303, 65537, 6, 524302, 65537, 6, 524301, 65537, 6, 589837, 65537, 6, 589836, 65537, 6, 589838, 65537, 5, 589839, 65537, 6, 589840, 65537, 5, 589841, 65537, 6, 589824, 65537, 6, 524288, 65537, 6, 458752, 65537, 6, 393216, 65537, 6, 393217, 65537, 6, 458753, 65537, 6, 524289, 65537, 6, 589825, 65537, 6, 589826, 65537, 6, 524290, 65537, 5, 458754, 65537, 6, 393218, 65537, 6, 393219, 65537, 6, 458755, 65537, 6, 524291, 65537, 6, 589827, 65537, 6, 589828, 65537, 6, 524292, 65537, 6, 458756, 65537, 6, 393220, 65537, 6, 393221, 65537, 6, 458757, 65537, 6, 458758, 65537, 5, 393222, 65537, 6, 393223, 65537, 6, 458759, 65537, 6, 458760, 65537, 6, 393224, 65537, 6, 393225, 65537, 6, 327689, 65537, 6, 327690, 65537, 6, 262154, 65537, 6, 262155, 65537, 6, 327691, 65537, 6, 393227, 65537, 6, 393226, 65537, 5, 458762, 65537, 6, 458761, 65537, 6, 458763, 65537, 6, 393228, 65537, 6, 327692, 65537, 5, 262156, 65537, 6, 262157, 65537, 6, 327693, 65537, 6, 327694, 65537, 5, 262158, 65537, 6, 262159, 65537, 6, 327695, 65537, 6, 327696, 65537, 6, 262160, 65537, 6, 262161, 65537, 6, 327697, 65537, 6, 327698, 65537, 6, 262162, 65537, 6) + +[node name="player" parent="." instance=ExtResource("2_gxv3v")] +position = Vector2(424, 344) diff --git a/scripts/ia/fsm.gd b/scripts/ia/fsm.gd new file mode 100644 index 0000000..f440d34 --- /dev/null +++ b/scripts/ia/fsm.gd @@ -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