From f2918cab11638025e14def5dafd5a3160f32506a Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 19 Aug 2020 11:56:10 +0200 Subject: [PATCH] Add LogEntity --- images/game1/log_h_1.tres | 7 ++ images/game1/log_h_2.tres | 7 ++ images/game1/log_h_3.tres | 7 ++ images/game1/log_h_4.tres | 7 ++ images/game1/log_v_1.tres | 7 ++ images/game1/log_v_2.tres | 7 ++ images/game1/log_v_3.tres | 7 ++ images/game1/log_v_4.tres | 7 ++ project.godot | 8 +- scenes/game1.tscn | 2 + scenes/game1/Player.tscn | 4 + scenes/game1/TestLogEntity.tscn | 54 ++++++++++++ scenes/game1/opening2_player.tscn | 3 +- scripts/game1/Game1Script.gd | 42 +++++++-- scripts/game1/LogEntity.gd | 141 ++++++++++++++++++++++++++++++ 15 files changed, 303 insertions(+), 7 deletions(-) create mode 100644 images/game1/log_h_1.tres create mode 100644 images/game1/log_h_2.tres create mode 100644 images/game1/log_h_3.tres create mode 100644 images/game1/log_h_4.tres create mode 100644 images/game1/log_v_1.tres create mode 100644 images/game1/log_v_2.tres create mode 100644 images/game1/log_v_3.tres create mode 100644 images/game1/log_v_4.tres create mode 100644 scenes/game1/TestLogEntity.tscn create mode 100644 scripts/game1/LogEntity.gd diff --git a/images/game1/log_h_1.tres b/images/game1/log_h_1.tres new file mode 100644 index 0000000..a8f1a71 --- /dev/null +++ b/images/game1/log_h_1.tres @@ -0,0 +1,7 @@ +[gd_resource type="AtlasTexture" load_steps=2 format=2] + +[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1] + +[resource] +atlas = ExtResource( 1 ) +region = Rect2( 0, 320, 32, 32 ) diff --git a/images/game1/log_h_2.tres b/images/game1/log_h_2.tres new file mode 100644 index 0000000..36f0d9a --- /dev/null +++ b/images/game1/log_h_2.tres @@ -0,0 +1,7 @@ +[gd_resource type="AtlasTexture" load_steps=2 format=2] + +[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1] + +[resource] +atlas = ExtResource( 1 ) +region = Rect2( 32, 320, 32, 32 ) diff --git a/images/game1/log_h_3.tres b/images/game1/log_h_3.tres new file mode 100644 index 0000000..0024e0e --- /dev/null +++ b/images/game1/log_h_3.tres @@ -0,0 +1,7 @@ +[gd_resource type="AtlasTexture" load_steps=2 format=2] + +[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1] + +[resource] +atlas = ExtResource( 1 ) +region = Rect2( 64, 319, 32, 32 ) diff --git a/images/game1/log_h_4.tres b/images/game1/log_h_4.tres new file mode 100644 index 0000000..4feac95 --- /dev/null +++ b/images/game1/log_h_4.tres @@ -0,0 +1,7 @@ +[gd_resource type="AtlasTexture" load_steps=2 format=2] + +[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1] + +[resource] +atlas = ExtResource( 1 ) +region = Rect2( 96, 320, 32, 32 ) diff --git a/images/game1/log_v_1.tres b/images/game1/log_v_1.tres new file mode 100644 index 0000000..1ddcde9 --- /dev/null +++ b/images/game1/log_v_1.tres @@ -0,0 +1,7 @@ +[gd_resource type="AtlasTexture" load_steps=2 format=2] + +[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1] + +[resource] +atlas = ExtResource( 1 ) +region = Rect2( 0, 352, 32, 32 ) diff --git a/images/game1/log_v_2.tres b/images/game1/log_v_2.tres new file mode 100644 index 0000000..27d25a2 --- /dev/null +++ b/images/game1/log_v_2.tres @@ -0,0 +1,7 @@ +[gd_resource type="AtlasTexture" load_steps=2 format=2] + +[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1] + +[resource] +atlas = ExtResource( 1 ) +region = Rect2( 32, 352, 32, 32 ) diff --git a/images/game1/log_v_3.tres b/images/game1/log_v_3.tres new file mode 100644 index 0000000..60d095a --- /dev/null +++ b/images/game1/log_v_3.tres @@ -0,0 +1,7 @@ +[gd_resource type="AtlasTexture" load_steps=2 format=2] + +[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1] + +[resource] +atlas = ExtResource( 1 ) +region = Rect2( 64, 352, 32, 32 ) diff --git a/images/game1/log_v_4.tres b/images/game1/log_v_4.tres new file mode 100644 index 0000000..ec4e75f --- /dev/null +++ b/images/game1/log_v_4.tres @@ -0,0 +1,7 @@ +[gd_resource type="AtlasTexture" load_steps=2 format=2] + +[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1] + +[resource] +atlas = ExtResource( 1 ) +region = Rect2( 96, 352, 32, 32 ) diff --git a/project.godot b/project.godot index ecd7335..82983d9 100644 --- a/project.godot +++ b/project.godot @@ -28,12 +28,18 @@ _global_script_classes=[ { "class": "IntroScript", "language": "GDScript", "path": "res://scripts/scenes/IntroScript.gd" +}, { +"base": "Sprite", +"class": "LogEntity", +"language": "GDScript", +"path": "res://scripts/game1/LogEntity.gd" } ] _global_script_class_icons={ "DialogueAction": "", "DialogueBox": "", "Flatfish": "", -"IntroScript": "" +"IntroScript": "", +"LogEntity": "" } [application] diff --git a/scenes/game1.tscn b/scenes/game1.tscn index 5279c40..81cc6b2 100644 --- a/scenes/game1.tscn +++ b/scenes/game1.tscn @@ -46,6 +46,8 @@ shader_param/particles_anim_v_frames = 11 shader_param/particles_anim_loop = true [sub_resource type="ParticlesMaterial" id=6] +emission_shape = 2 +emission_box_extents = Vector3( 10, 10, 1 ) flag_disable_z = true direction = Vector3( -1, 0, 0 ) spread = 5.0 diff --git a/scenes/game1/Player.tscn b/scenes/game1/Player.tscn index f5a97b2..0217071 100644 --- a/scenes/game1/Player.tscn +++ b/scenes/game1/Player.tscn @@ -14,6 +14,8 @@ offset = Vector2( 8, 0 ) [node name="Right" type="Area2D" parent="Sensors"] position = Vector2( 16, 0 ) +collision_layer = 0 +collision_mask = 3 [node name="CollisionShape2D" type="CollisionShape2D" parent="Sensors/Right"] scale = Vector2( 0.5, 0.5 ) @@ -21,6 +23,8 @@ shape = SubResource( 1 ) [node name="Left" type="Area2D" parent="Sensors"] position = Vector2( -16, 0 ) +collision_layer = 0 +collision_mask = 3 [node name="CollisionShape2D" type="CollisionShape2D" parent="Sensors/Left"] scale = Vector2( 0.5, 0.5 ) diff --git a/scenes/game1/TestLogEntity.tscn b/scenes/game1/TestLogEntity.tscn new file mode 100644 index 0000000..d6dd0a5 --- /dev/null +++ b/scenes/game1/TestLogEntity.tscn @@ -0,0 +1,54 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://images/game1/tileset.tres" type="TileSet" id=1] + +[node name="TestLogEntity" type="Node2D"] + +[node name="Background" type="TileMap" parent="."] +tile_set = ExtResource( 1 ) +cell_size = Vector2( 16, 16 ) +cell_quadrant_size = 8 +cell_custom_transform = Transform2D( 0, 0, 0, 0, 0, 0 ) +format = 1 +tile_data = PoolIntArray( -131079, 0, 0, -131078, 0, 0, -131077, 0, 0, -131076, 0, 0, -131075, 536870912, 0, -131074, 0, 0, -131073, 0, 0, -196608, 0, 0, -196607, 0, 0, -196606, 0, 0, -196605, 0, 0, -196604, 0, 0, -196603, 0, 0, -196602, 0, 0, -196601, 0, 0, -196600, 0, 0, -196599, 0, 0, -196598, 0, 0, -196597, 0, 0, -196596, 0, 0, -196595, 0, 0, -196594, 0, 0, -196593, 0, 0, -196592, 0, 0, -196591, 0, 0, -196590, 0, 0, -196589, 0, 0, -196588, 0, 0, -196587, 0, 0, -196586, 0, 0, -196585, 0, 0, -65543, 0, 0, -65542, 0, 0, -65541, 0, 0, -65540, 0, 0, -65539, 536870912, 0, -65538, 0, 0, -65537, 0, 0, -131072, 0, 0, -131071, 0, 0, -131070, 0, 0, -131069, 0, 0, -131068, 0, 0, -131067, 0, 0, -131066, 0, 0, -131065, 0, 0, -131064, 0, 0, -131063, 0, 0, -131062, 0, 0, -131061, 0, 0, -131060, 0, 0, -131059, 0, 0, -131058, 0, 0, -131057, 0, 0, -131056, 0, 0, -131055, 0, 0, -131054, 0, 0, -131053, 0, 0, -131052, 0, 0, -131051, 0, 0, -131050, 0, 0, -131049, 0, 0, -7, 0, 0, -6, 0, 0, -5, 0, 0, -4, 536870912, 0, -3, 536870912, 0, -2, 0, 0, -1, 0, 0, -65536, 0, 0, -65535, 0, 0, -65534, 0, 0, -65533, 0, 0, -65532, 0, 0, -65531, 0, 0, -65530, 0, 0, -65529, 0, 0, -65528, 0, 0, -65527, 0, 0, -65526, 0, 0, -65525, 0, 0, -65524, 0, 0, -65523, 0, 0, -65522, 0, 0, -65521, 0, 0, -65520, 0, 0, -65519, 0, 0, -65518, 0, 0, -65517, 0, 0, -65516, 0, 0, -65515, 0, 0, -65514, 0, 0, -65513, 0, 0, 65529, 0, 0, 65530, 0, 0, 65531, 0, 0, 65532, 0, 0, 65533, 0, 0, 65534, 0, 0, 65535, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0, 11, 0, 0, 12, 0, 0, 13, 0, 0, 14, 0, 0, 15, 0, 0, 16, 0, 0, 17, 0, 0, 18, 0, 0, 19, 0, 0, 20, 0, 0, 21, 0, 0, 22, 0, 0, 23, 0, 0, 131065, 0, 0, 131066, 0, 0, 131067, 0, 0, 131068, 0, 0, 131069, 0, 0, 131070, 0, 0, 131071, 0, 0, 65536, 0, 0, 65537, 0, 0, 65538, 0, 0, 65539, 0, 0, 65540, 0, 0, 65541, 0, 0, 65542, 0, 0, 65543, 0, 0, 65544, 0, 0, 65545, 0, 0, 65546, 0, 0, 65547, 0, 0, 65548, 0, 0, 65549, 0, 0, 65550, 0, 0, 65551, 0, 0, 65552, 0, 0, 65553, 0, 0, 65554, 0, 0, 65555, 0, 0, 65556, 0, 0, 65557, 0, 0, 65558, 0, 0, 65559, 0, 0, 196601, 0, 0, 196602, 0, 0, 196603, 0, 0, 196604, 0, 0, 196605, 0, 0, 196606, 0, 0, 196607, 0, 0, 131072, 0, 0, 131073, 0, 0, 131074, 0, 0, 131075, 0, 0, 131076, 0, 0, 131077, 0, 0, 131078, 0, 0, 131079, 0, 0, 131080, 0, 0, 131081, 0, 0, 131082, 0, 0, 131083, 0, 0, 131084, 0, 0, 131085, 0, 0, 131086, 0, 0, 131087, 0, 0, 131088, 0, 0, 131089, 0, 0, 131090, 0, 0, 131091, 0, 0, 131092, 0, 0, 131093, 0, 0, 131094, 0, 0, 131095, 0, 0, 262137, 0, 0, 262138, 0, 0, 262139, 0, 0, 262140, 0, 0, 262141, 0, 0, 262142, 0, 0, 262143, 0, 0, 196608, 0, 0, 196609, 0, 0, 196610, 0, 0, 196611, 0, 0, 196612, 0, 0, 196613, 0, 0, 196614, 0, 0, 196615, 0, 0, 196616, 0, 0, 196617, 0, 0, 196618, 0, 0, 196619, 0, 0, 196620, 0, 0, 196621, 0, 0, 196622, 0, 0, 196623, 0, 0, 196624, 0, 0, 196625, 0, 0, 196626, 0, 0, 196627, 0, 0, 196628, 0, 0, 196629, 0, 0, 196630, 0, 0, 196631, 0, 0, 327673, 0, 0, 327674, 0, 0, 327675, 0, 0, 327676, 0, 0, 327677, 0, 0, 327678, 0, 0, 327679, 0, 0, 262144, 0, 0, 262145, 0, 0, 262146, 0, 0, 262147, 0, 0, 262148, 0, 0, 262149, 0, 0, 262150, 0, 0, 262151, 0, 0, 262152, 0, 0, 262153, 0, 0, 262154, 0, 0, 262155, 0, 0, 262156, 0, 0, 262157, 0, 0, 262158, 0, 0, 262159, 0, 0, 262160, 0, 0, 262161, 0, 0, 262162, 0, 0, 262163, 0, 0, 262164, 0, 0, 262165, 0, 0, 262166, 0, 0, 262167, 0, 0, 393209, 0, 0, 393210, 0, 0, 393211, 0, 0, 393212, 0, 0, 393213, 0, 0, 393214, 0, 0, 393215, 0, 0, 327680, 0, 0, 327681, 0, 0, 327682, 0, 0, 327683, 0, 0, 327684, 0, 0, 327685, 0, 0, 327686, 0, 0, 327687, 0, 0, 327688, 0, 0, 327689, 0, 0, 327690, 0, 0, 327691, 0, 0, 327692, 0, 0, 327693, 0, 0, 327694, 0, 0, 327695, 0, 0, 327696, 0, 0, 327697, 0, 0, 327698, 0, 0, 327699, 0, 0, 327700, 0, 0, 327701, 0, 0, 327702, 0, 0, 327703, 0, 0, 458745, 0, 0, 458746, 0, 0, 458747, 0, 0, 458748, 0, 0, 458749, 0, 0, 458750, 0, 0, 458751, 0, 0, 393216, 0, 0, 393217, 0, 0, 393218, 0, 0, 393219, 0, 0, 393220, 0, 0, 393221, 0, 0, 393222, 0, 0, 393223, 0, 0, 393224, 0, 0, 393225, 0, 0, 393226, 0, 0, 393227, 0, 0, 393228, 0, 0, 393229, 0, 0, 393230, 0, 0, 393231, 0, 0, 393232, 0, 0, 393233, 0, 0, 393234, 0, 0, 393235, 0, 0, 393236, 0, 0, 393237, 0, 0, 393238, 0, 0, 393239, 0, 0 ) + +[node name="Ground" type="TileMap" parent="."] +tile_set = ExtResource( 1 ) +cell_size = Vector2( 16, 16 ) +cell_quadrant_size = 8 +cell_custom_transform = Transform2D( 0, 0, 0, 0, 0, 0 ) +format = 1 +tile_data = PoolIntArray( -262141, 57, 0, -131078, 49, 2, -196605, 49, 65554, -196597, 49, 15, -196596, 49, 65554, -196593, 49, 11, -196592, 49, 65542, -196591, 49, 65545, -196590, 49, 4, -196587, 49, 15, -131069, 49, 65537, -131068, 49, 65550, -131056, 49, 65540, -131052, 49, 13, -131051, 49, 65549, -1, 49, 65551, -65531, 49, 4, -65524, 59, 0, -65516, 49, 17, 2, 49, 65549, 19, 49, 65538, 20, 49, 13, 23, 49, 65546, 65542, 49, 65542, 65543, 49, 5, 65548, 59, 0, 65551, 49, 65546, 65555, 49, 6, 65558, 49, 65551, 65559, 49, 17, 196605, 49, 65554, 196606, 49, 65537, 196607, 49, 12, 131075, 49, 65555, 131078, 49, 13, 131081, 49, 15, 131092, 49, 65542, 131093, 49, 65537, 131094, 49, 65545, 196610, 49, 65543, 196620, 58, 0, 196630, 49, 16, 262146, 49, 65549, 262147, 59, 0, 262148, 49, 16, 262156, 49, 10, 262157, 49, 4, 262161, 49, 2, 262165, 49, 10, 262166, 49, 14, 262167, 49, 14, 393210, 49, 65548, 393212, 49, 65544, 327691, 49, 65553, 327692, 58, 0, 327695, 49, 65548, 327696, 49, 10, 327703, 49, 5, 458746, 49, 14, 458747, 49, 65544, 458749, 49, 65548, 393219, 55, 0, 393227, 49, 65539, 393231, 49, 9, 393237, 49, 65547, 393238, 49, 14 ) + +[node name="LogHoles" type="TileMap" parent="."] +tile_set = ExtResource( 1 ) +cell_size = Vector2( 16, 16 ) +collision_layer = 2 +collision_mask = 0 +format = 1 +tile_data = PoolIntArray( -720893, 68, 0, -65528, -1073741756, 0, 196603, 26, 0, 196604, 26, 0, 327675, -1073741758, 0, 458750, 55, 0, 524286, 55, 0, 786435, 67, 0 ) + +[node name="Special" type="TileMap" parent="."] +tile_set = ExtResource( 1 ) +cell_size = Vector2( 16, 16 ) +cell_quadrant_size = 8 +cell_custom_transform = Transform2D( 0, 0, 0, 0, 0, 0 ) +format = 1 +tile_data = PoolIntArray( -327670, 55, 0, -327668, 55, 0, -327666, 57, 0, -327664, 57, 0, -196598, 5, 0, -196597, 5, 0, -196596, 5, 0, -196595, 5, 0, -196594, 5, 0, -196593, 5, 0, -196592, 5, 0, -196591, 5, 0, -196590, 5, 0, -2, 5, 0, -65532, 5, 0, -65526, 5, 0, 65534, 5, 0, 0, 71, 0, 10, 5, 0, 131070, 5, 0, 65546, 5, 0, 196606, 5, 0, 131076, 5, 0, 131082, 5, 0, 262142, 5, 0, 196618, 5, 0, 196627, 72, 0, 327678, 5, 0, 262154, 5, 0, 393214, 5, 0, 327684, 5, 0, 327690, 5, 0 ) + +[node name="Props" type="TileMap" parent="."] +tile_set = ExtResource( 1 ) +cell_size = Vector2( 16, 16 ) +cell_quadrant_size = 8 +cell_custom_transform = Transform2D( 0, 0, 0, 0, 0, 0 ) +format = 1 +tile_data = PoolIntArray( -458736, 50, 0, -393204, 50, 0, -393202, 50, 0, -327670, 50, 0, -65535, 26, 0, -65523, 50, 0, 65550, 50, 0, 196622, 50, 0, 262163, 14, 0, 327687, 40, 0, 327695, 50, 0, 327697, 40, 0, 393224, 41, 0, 393227, 18, 0, 393229, 42, 0, 393232, 39, 0 ) + +[node name="Air" type="TileMap" parent="."] +z_index = 1 +tile_set = ExtResource( 1 ) +cell_size = Vector2( 16, 16 ) +cell_quadrant_size = 8 +cell_custom_transform = Transform2D( 0, 0, 0, 0, 0, 0 ) +format = 1 +tile_data = PoolIntArray( 18, 8, 0, 65551, 32, 0 ) diff --git a/scenes/game1/opening2_player.tscn b/scenes/game1/opening2_player.tscn index b8286fc..d227307 100644 --- a/scenes/game1/opening2_player.tscn +++ b/scenes/game1/opening2_player.tscn @@ -4,7 +4,7 @@ [ext_resource path="res://scripts/game1/PlayerController.gd" type="Script" id=2] [sub_resource type="RectangleShape2D" id=1] -extents = Vector2( 10, 4.01179 ) +extents = Vector2( 10, 4.5 ) [node name="Player" type="RigidBody2D"] position = Vector2( 1, -1 ) @@ -14,6 +14,7 @@ linear_damp = 25.0 script = ExtResource( 2 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( 0, 0.5 ) shape = SubResource( 1 ) [node name="Sprite" type="AnimatedSprite" parent="."] diff --git a/scripts/game1/Game1Script.gd b/scripts/game1/Game1Script.gd index 9763777..22b07e1 100644 --- a/scripts/game1/Game1Script.gd +++ b/scripts/game1/Game1Script.gd @@ -8,6 +8,7 @@ var player : AnimatedSprite var player_camera : Camera2D onready var levels = [ + #'TestLogEntity', 'Level1', 'Level2', 'Level3', @@ -20,6 +21,7 @@ var spawn_pos = Vector2() var exit_points = [] func _ready(): + start_game() $Opening1.play('default') yield($Opening1, 'animation_finished') $Opening1.queue_free() @@ -90,7 +92,15 @@ func spawn_entities(level : Node): # Exit points for cell in tm.get_used_cells_by_id(tileset.find_tile_by_name('finish')): exit_points.append(cell) - + + # Logs + for d in ['horizontal', 'vertical']: + for i in [1, 2, 3, 4]: + for cell in tm.get_used_cells_by_id(tileset.find_tile_by_name('log_' + d + '_' + String(i))): + var log_entity = LogEntity.new(LogEntity.Type.H if d == 'horizontal' else LogEntity.Type.V, i - 1) + log_entity.spawn_at(cell * 16 + Vector2(16, 16)) + entities.add_child(log_entity) + tm.set_cell(cell.x, cell.y, -1) var go_right = false var go_left = false @@ -131,7 +141,31 @@ func _process(delta): func can_move_towards(sensorName : String) -> bool: var sensor = player.get_node("Sensors/" + sensorName) as Area2D - return sensor.get_overlapping_bodies().size() == 0 && sensor.get_overlapping_areas().size() == 0 + var bodies = sensor.get_overlapping_bodies() + var areas = sensor.get_overlapping_areas() + + # Check for pushables and push + for body in bodies + areas: + var p = body.get_parent() + if p is LogEntity: + var rot = int(floor(player.rotation_degrees / 90.0)) % 4 + var dir = 1 if (rot == 0 || rot == 1) && sensorName == 'Right' || (rot == 2 || rot == 3) && sensorName == 'Left' else -1 + + # Push and fall + if p.push((player.position / 16) - Vector2(0.5, 0.5), dir): + var level = $Game.get_node(levels[current_level]) + var pos = (p.position / 16) - Vector2(1, 1) + for layer in level.get_children(): + if !(layer is TileMap): continue + var tm = layer as TileMap + var tileset = tm.tile_set + + var name = tileset.tile_get_name(tm.get_cellv(pos)) + if name != null && name.rfind('hole_') == 0: + tm.set_cellv(pos, tileset.find_tile_by_name("log_in_" + name), tm.is_cell_x_flipped(pos.x, pos.y), tm.is_cell_y_flipped(pos.x, pos.y), tm.is_cell_transposed(pos.x, pos.y)) + p.queue_free() + + return bodies.size() == 0 && areas.size() == 0 func move_right(): is_camera_moving = true @@ -164,7 +198,7 @@ func check_exit() -> bool: print(player_pos) for exit in exit_points: if player_pos == exit: - print('OK') + print('exit') next_level() return true return false @@ -175,8 +209,6 @@ func offset_player(offset): elif r == 1: player.position.y += offset elif r == 2: player.position.x -= offset elif r == 3: player.position.y -= offset - - print((player.position - Vector2(8, 8)) / 16) const animation_steps = [3, 6, 10, 13] var is_camera_moving = false diff --git a/scripts/game1/LogEntity.gd b/scripts/game1/LogEntity.gd new file mode 100644 index 0000000..d2cffa2 --- /dev/null +++ b/scripts/game1/LogEntity.gd @@ -0,0 +1,141 @@ +extends Sprite + +class_name LogEntity + +const h = [ + preload('res://images/game1/log_h_1.tres'), + preload('res://images/game1/log_h_2.tres'), + preload('res://images/game1/log_h_3.tres'), + preload('res://images/game1/log_h_4.tres'), +] +const v = [ + preload('res://images/game1/log_v_1.tres'), + preload('res://images/game1/log_v_2.tres'), + preload('res://images/game1/log_v_3.tres'), + preload('res://images/game1/log_v_4.tres'), +] + +var type : int +var index : int +var collision_area : Area2D +var back_sensor : Area2D +var front_sensor : Area2D + +func _init(_type=Type.H, _rotation=0): + type = _type + index = _rotation + + var physics_shape = Vector2(2 if type == Type.H else 1, 2 if type == Type.V else 1) + var box_offset = Vector2(0, 8) if type == Type.H else Vector2(8, 0) + + # Collision box + collision_area = Area2D.new() + var shape = CollisionShape2D.new() + shape.shape = RectangleShape2D.new() + shape.shape.extents = Vector2(8, 8) * physics_shape + collision_area.add_child(shape) + collision_area.position = box_offset + add_child(collision_area) + + # Sensors + back_sensor = Area2D.new() + shape = CollisionShape2D.new() + shape.shape = RectangleShape2D.new() + shape.shape.extents = Vector2(6, 6) * physics_shape + back_sensor.add_child(shape) + back_sensor.position = box_offset + add_child(back_sensor) + + front_sensor = Area2D.new() + shape = CollisionShape2D.new() + shape.shape = RectangleShape2D.new() + shape.shape.extents = Vector2(6, 6) * physics_shape + front_sensor.add_child(shape) + front_sensor.position = box_offset + add_child(front_sensor) + + if type == Type.H: + back_sensor.position.y -= 16 + front_sensor.position.y += 16 + else: + back_sensor.position.x -= 16 + front_sensor.position.x += 16 + + front_sensor.set_collision_layer_bit(0, 0) + front_sensor.set_collision_mask_bit(0, 1) + front_sensor.set_collision_mask_bit(1, 0) + back_sensor.set_collision_layer_bit(0, 0) + back_sensor.set_collision_mask_bit(0, 1) + back_sensor.set_collision_mask_bit(1, 0) + + update() + +func spawn_at(pos : Vector2): + position = pos + + # Offset for special i=2 + if index == 2: + if type == Type.H: position.y -= 16 + else: position.x -= 16 + +func update(): + var atlas = h + if type == Type.V: + atlas = v + + texture = atlas[index] + + if type == Type.H: + if index == 2: + offset.y += 16 + else: + offset.y = 0 + else: + if index == 2: + offset.x += 16 + else: + offset.x = 0 + +func push(from : Vector2, direction=1) -> bool: + var sensor = front_sensor if direction == 1 else back_sensor + if sensor.get_overlapping_bodies().size() > 0 || sensor.get_overlapping_areas().size() > 0: + return false + + #Check if push is possible + var authorized_positions = [] + var pos = (position / 16) - Vector2(1, 1) + if type == Type.H: + if direction == -1: + pos += Vector2(0, 2) + authorized_positions.append(pos) + authorized_positions.append(pos + Vector2(1, 0)) + elif type == Type.V: + if direction == -1: + pos += Vector2(2, 0) + authorized_positions.append(pos) + authorized_positions.append(pos + Vector2(0, 1)) + + var authorized = false + for p in authorized_positions: + if from == p: + authorized = true + break + if !authorized: return false + + index += direction + if index < 0: + index = 3 + if index > 3: + index = 0 + + if type == Type.H: + position.y += 16 * direction + else: + position.x += 16 * direction + + #todo: fall in hole + + update() + return true + +enum Type {H, V}