From dc3f9803137afb497f891f5f459ec242f579c2e6 Mon Sep 17 00:00:00 2001 From: Fris0uman Date: Sat, 21 Jan 2023 15:26:43 +0100 Subject: [PATCH 1/3] Add a new type of ressource --- assets/Scenes/Ressource_items/Fuel.tscn | 23 ++++++++++++ assets/Scenes/Ressource_items/Scrap.tscn | 24 ++++++++++++ assets/Scenes/World.tscn | 8 ++-- assets/cdda_sprites/e_scrap_season_winter.png | Bin 0 -> 279 bytes .../e_scrap_season_winter.png.import | 35 ++++++++++++++++++ src/AI_core.gd | 23 +++++++----- src/Body.gd | 4 ++ src/World.gd | 11 +++++- 8 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 assets/Scenes/Ressource_items/Fuel.tscn create mode 100644 assets/Scenes/Ressource_items/Scrap.tscn create mode 100644 assets/cdda_sprites/e_scrap_season_winter.png create mode 100644 assets/cdda_sprites/e_scrap_season_winter.png.import diff --git a/assets/Scenes/Ressource_items/Fuel.tscn b/assets/Scenes/Ressource_items/Fuel.tscn new file mode 100644 index 0000000..6bdb1b3 --- /dev/null +++ b/assets/Scenes/Ressource_items/Fuel.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://assets/cdda_sprites/30gal_drum_winter.png" type="Texture" id=1] +[ext_resource path="res://src/Body.gd" type="Script" id=2] + +[sub_resource type="CapsuleShape2D" id=1] +radius = 4.5 +height = 6.0 + +[node name="Body" type="RigidBody2D" groups=["Ressource"]] +z_index = 1 +collision_layer = 7 +mass = 40.0 +script = ExtResource( 2 ) +_body_size = 11 +_flags = [ "Fuel" ] + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 1 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( -0.5, 1.5 ) +shape = SubResource( 1 ) diff --git a/assets/Scenes/Ressource_items/Scrap.tscn b/assets/Scenes/Ressource_items/Scrap.tscn new file mode 100644 index 0000000..dd76976 --- /dev/null +++ b/assets/Scenes/Ressource_items/Scrap.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://assets/cdda_sprites/e_scrap_season_winter.png" type="Texture" id=1] +[ext_resource path="res://src/Body.gd" type="Script" id=2] + +[sub_resource type="CapsuleShape2D" id=1] +radius = 4.5 +height = 6.0 + +[node name="Body" type="RigidBody2D" groups=["Ressource"]] +z_index = 1 +collision_layer = 7 +mass = 40.0 +script = ExtResource( 2 ) +_body_size = 11 +_flags = [ "Scrap" ] + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 1 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( -0.5, 1.5 ) +rotation = 1.5708 +shape = SubResource( 1 ) diff --git a/assets/Scenes/World.tscn b/assets/Scenes/World.tscn index 38b52c2..17da047 100644 --- a/assets/Scenes/World.tscn +++ b/assets/Scenes/World.tscn @@ -1,12 +1,13 @@ -[gd_scene load_steps=32 format=2] +[gd_scene load_steps=33 format=2] [ext_resource path="res://assets/Scenes/Player.tscn" type="PackedScene" id=1] -[ext_resource path="res://assets/Scenes/Body.tscn" type="PackedScene" id=2] +[ext_resource path="res://assets/Scenes/Ressource_items/Fuel.tscn" type="PackedScene" id=2] [ext_resource path="res://src/World.gd" type="Script" id=3] [ext_resource path="res://assets/Scenes/Robot.tscn" type="PackedScene" id=4] [ext_resource path="res://assets/cdda_sprites/HollowMoon_tilesheet.png" type="Texture" id=5] [ext_resource path="res://assets/Scenes/AI_core.tscn" type="PackedScene" id=6] [ext_resource path="res://src/Main_camera.gd" type="Script" id=7] +[ext_resource path="res://assets/Scenes/Ressource_items/Scrap.tscn" type="PackedScene" id=8] [sub_resource type="ConvexPolygonShape2D" id=3] points = PoolVector2Array( 0, 0, 24, 0, 24, 24, 0, 24 ) @@ -285,7 +286,8 @@ points = PoolVector2Array( 0, 0, 24, 0, 24, 24, 0, 24 ) [node name="World" type="Node"] script = ExtResource( 3 ) -ressource_template = ExtResource( 2 ) +ressource_fuel = ExtResource( 2 ) +ressource_scrap = ExtResource( 8 ) [node name="TileMap" type="TileMap" parent="."] position = Vector2( -1, -1 ) diff --git a/assets/cdda_sprites/e_scrap_season_winter.png b/assets/cdda_sprites/e_scrap_season_winter.png new file mode 100644 index 0000000000000000000000000000000000000000..a41c3f7a7ee51a0a1e5804eba341585a7b0b9d43 GIT binary patch literal 279 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM1|%Pp+x`Gj_dH!3Ln`JZ|M~ylo>`Ys*w?5Y#4{vn_X!2Qk}f zUvEEf^TeNjvuxOABu{&D>zn*+RhGvL(;pfH+%V!?|L@@ZuAZ|Y8k-MjUT9Ce|4Y~6 z=iRBoAEbe1*Ngmle}Dh~Ks~wt|Ns7W7jWNMap3Br+02J6f(0F?J06@Ad;7yP);VG^ zHoLnuxH*_g^aZ7DP8@8}uK4%=|M>$QYePE9zN+k65wWY<`raa@)vQ-on(Y_!?qFq@ YxaxN0@g+aE1AWTi>FVdQ&MBb@0Ja@{^Z)<= literal 0 HcmV?d00001 diff --git a/assets/cdda_sprites/e_scrap_season_winter.png.import b/assets/cdda_sprites/e_scrap_season_winter.png.import new file mode 100644 index 0000000..2fb62da --- /dev/null +++ b/assets/cdda_sprites/e_scrap_season_winter.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/e_scrap_season_winter.png-fcb5c7e800676a4d76cb83e16a54d30b.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://assets/cdda_sprites/e_scrap_season_winter.png" +dest_files=[ "res://.import/e_scrap_season_winter.png-fcb5c7e800676a4d76cb83e16a54d30b.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=false +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/src/AI_core.gd b/src/AI_core.gd index 040c49f..4f2e83a 100644 --- a/src/AI_core.gd +++ b/src/AI_core.gd @@ -1,6 +1,7 @@ extends Body -var _ressources: Array +var ressources_fuel: Array +var ressources_scrap: Array var robot_template:= preload("res://assets/Scenes/Robot.tscn") @@ -14,7 +15,7 @@ func _ready() -> void: place_depot() func _process(_delta: float) -> void: - if _ressources.size() >=2: + if ressources_scrap.size() >=2: make_new_bot() func place_depot()->void: @@ -30,11 +31,15 @@ func place_depot()->void: map.set_cellv(vect,1) +func sort_ressource(body:Body)->void: + if body.has_flag("Fuel"): + ressources_fuel.append(body) + elif body.has_flag("Scrap"): + ressources_scrap.append(body) + func update_ressources_in_depot()->void: for body in depot.get_overlapping_bodies(): - if body.is_in_group("Ressource"): - _ressources.append(body) - + sort_ressource(body) func _on_Ressource_depot_body_entered(body: RigidBody2D) -> void: #Make robot release the ressource and go back fetching more @@ -49,7 +54,7 @@ func _on_Ressource_depot_body_entered(body: RigidBody2D) -> void: body.set_modulate(Color.dimgray) - _ressources.append(body) + sort_ressource(body) func make_new_bot()->void: var new_robot = robot_template.instance() @@ -58,7 +63,7 @@ func make_new_bot()->void: world.connect_to_actor(new_robot) for k in [1,2]: - _ressources.front().clear_all_grab() - _ressources.front().queue_free() - _ressources.pop_front() + ressources_scrap.front().clear_all_grab() + ressources_scrap.front().queue_free() + ressources_scrap.pop_front() world.total_ressources -=1 diff --git a/src/Body.gd b/src/Body.gd index 9ba7c7d..769b987 100644 --- a/src/Body.gd +++ b/src/Body.gd @@ -8,6 +8,7 @@ var grab_force_vector:= Vector2.ZERO var contact_point: Vector2 export var _body_size:= 20 +export var _flags: Array func _physics_process(_delta: float) -> void: apply_all_grabs() @@ -45,3 +46,6 @@ func is_grabbed()->bool: func get_grabbers()->Array: return _grabbers + +func has_flag(flag: String)->bool: + return _flags.has(flag) diff --git a/src/World.gd b/src/World.gd index bd32bae..e44df2f 100644 --- a/src/World.gd +++ b/src/World.gd @@ -9,7 +9,8 @@ var MAX_Y: = 3584.0 var MIN_X: = -2752.0 var MAX_X: = 6400.0 -export var ressource_template: PackedScene +export var ressource_fuel: PackedScene +export var ressource_scrap: PackedScene onready var timer:= $Timer @@ -42,7 +43,13 @@ func _on_grab_release(grabbed: RigidBody2D, emitter: RigidBody2D): grabbed.be_released(emitter) func _add_ressource()-> void: - var new_ressource = ressource_template.instance() + var coin_flip := randi() % 2 + 1 + var new_ressource: Body + if coin_flip > 1: + new_ressource = ressource_fuel.instance() + else: + new_ressource = ressource_scrap.instance() + var rand_x = rand_range(MIN_X,MAX_X) var rand_Y = rand_range(MIN_Y,MAX_Y) new_ressource.position = Vector2(rand_x,rand_Y) From f8de9da4258ab43bdcc4bc70037ff2818dd67b96 Mon Sep 17 00:00:00 2001 From: Fris0uman Date: Sat, 21 Jan 2023 18:22:23 +0100 Subject: [PATCH 2/3] Fleeing scrap --- assets/Scenes/Ressource_items/Live_Scrap.tscn | 44 +++++++++ assets/Scenes/World.tscn | 6 +- src/Prey.gd | 91 +++++++++++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 assets/Scenes/Ressource_items/Live_Scrap.tscn create mode 100644 src/Prey.gd diff --git a/assets/Scenes/Ressource_items/Live_Scrap.tscn b/assets/Scenes/Ressource_items/Live_Scrap.tscn new file mode 100644 index 0000000..aa2301c --- /dev/null +++ b/assets/Scenes/Ressource_items/Live_Scrap.tscn @@ -0,0 +1,44 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://src/Prey.gd" type="Script" id=1] +[ext_resource path="res://assets/cdda_sprites/e_scrap_season_winter.png" type="Texture" id=2] + +[sub_resource type="CapsuleShape2D" id=1] +radius = 5.0 +height = 4.0 + +[sub_resource type="CircleShape2D" id=2] +radius = 51.0098 + +[node name="Prey" type="RigidBody2D" groups=["Ressource"]] +z_index = 1 +collision_layer = 5 +mode = 2 +mass = 70.0 +linear_damp = 2.0 +script = ExtResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 2 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( -1, 1 ) +rotation = 1.5708 +shape = SubResource( 1 ) + +[node name="RayCast2D" type="RayCast2D" parent="."] +enabled = true +collision_mask = 4 + +[node name="Area2D" type="Area2D" parent="."] + +[node name="AwarnessShape2D" type="CollisionShape2D" parent="Area2D"] +position = Vector2( -1, 1 ) +shape = SubResource( 2 ) + +[node name="NavigationAgent2D" type="NavigationAgent2D" parent="."] +avoidance_enabled = true +radius = 12.0 +neighbor_dist = 32.0 + +[node name="NavigationObstacle2D" type="NavigationObstacle2D" parent="."] diff --git a/assets/Scenes/World.tscn b/assets/Scenes/World.tscn index 17da047..fd4ab7e 100644 --- a/assets/Scenes/World.tscn +++ b/assets/Scenes/World.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=33 format=2] +[gd_scene load_steps=34 format=2] [ext_resource path="res://assets/Scenes/Player.tscn" type="PackedScene" id=1] [ext_resource path="res://assets/Scenes/Ressource_items/Fuel.tscn" type="PackedScene" id=2] @@ -8,6 +8,7 @@ [ext_resource path="res://assets/Scenes/AI_core.tscn" type="PackedScene" id=6] [ext_resource path="res://src/Main_camera.gd" type="Script" id=7] [ext_resource path="res://assets/Scenes/Ressource_items/Scrap.tscn" type="PackedScene" id=8] +[ext_resource path="res://assets/Scenes/Ressource_items/Live_Scrap.tscn" type="PackedScene" id=9] [sub_resource type="ConvexPolygonShape2D" id=3] points = PoolVector2Array( 0, 0, 24, 0, 24, 24, 0, 24 ) @@ -314,4 +315,7 @@ script = ExtResource( 7 ) wait_time = 20.0 autostart = true +[node name="Actor" parent="." instance=ExtResource( 9 )] +position = Vector2( 712, 488 ) + [connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"] diff --git a/src/Prey.gd b/src/Prey.gd new file mode 100644 index 0000000..15ba0a5 --- /dev/null +++ b/src/Prey.gd @@ -0,0 +1,91 @@ +extends Actor + +var _current_path: PoolVector2Array +var _curr_dest: Vector2 +var _heading: float + +var _bodies_fleeing_from: Array + +export var _step_size:= 40 + +onready var awarness_area:= $Area2D +onready var nav_agent:= $NavigationAgent2D + +onready var map := get_node("../TileMap") + + +func _ready() -> void: + set_process(true) + + awarness_area.connect("body_entered",self, "_on_body_entered") + awarness_area.connect("body_exited",self,"_on_body_exited") + nav_agent.connect("velocity_computed", self, "move") + + pick_heading() + +func _physics_process(_delta: float) -> void: + if _bodies_fleeing_from.empty(): + return + + if _current_path.empty(): + new_path() + + var next_location= _current_path[0] + var direction:= position.direction_to(next_location) + var target_vel:= direction * _speed + + nav_agent.set_velocity(target_vel) + + if close_enough(next_location): + _current_path.remove(0) + +func _on_body_entered(body: Body)->void: + if body == null || !body.is_in_group("Actor"): + return + + _bodies_fleeing_from.append(body) + new_path() + + +func _on_body_exited(body: Body)->void: + if body == null || !body.is_in_group("Actor"): + return + _bodies_fleeing_from.erase(body) + +func pick_fleeing_heading()->void: + var chaser_vector:= Vector2.ZERO + for body in _bodies_fleeing_from: + chaser_vector += position.direction_to(body.position) + _heading = acos( - chaser_vector.x ) + +func new_path()->void: + pick_fleeing_heading() + _curr_dest = pick_destination() + _current_path = make_path(_curr_dest) + +func make_path(destination: Vector2)->PoolVector2Array: + var path = Navigation2DServer.map_get_path(nav_agent.get_navigation_map() ,position,destination, false) + return path + + +func pick_destination()->Vector2: + var dest:= position + var cell_index:= -1 + while cell_index != 1: + var rand_angle:= rand_range(_heading-PI/3, _heading+ PI/3) + var rand_vector:= Vector2(cos(rand_angle),sin(rand_angle)) + dest = global_position + rand_vector * _step_size + cell_index = map.get_cellv(map.world_to_map(dest)) + + return dest + +func pick_heading(): + _heading = rand_range(0,2*PI) + +func close_enough(vect: Vector2, bod = null)->bool: + if bod == null: + return position.distance_to(vect)<=_body_size/2.0 + else: + var bod_size= bod.get_body_size() + var dist:= position.distance_to(vect) + return dist <= (bod_size + _body_size)/2 + GRAB_DIST From a4fe1b0cdbf5153946cdb285a37c18d3f9f322dc Mon Sep 17 00:00:00 2001 From: Fris0uman Date: Sat, 21 Jan 2023 20:17:16 +0100 Subject: [PATCH 3/3] Fleeing scraps improvements --- assets/Scenes/Ressource_items/Live_Scrap.tscn | 5 +++-- assets/Scenes/World.tscn | 1 + src/AI_core.gd | 5 +++++ src/Prey.gd | 2 +- src/Robot.gd | 2 +- src/World.gd | 7 ++++++- 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/assets/Scenes/Ressource_items/Live_Scrap.tscn b/assets/Scenes/Ressource_items/Live_Scrap.tscn index aa2301c..7038f7a 100644 --- a/assets/Scenes/Ressource_items/Live_Scrap.tscn +++ b/assets/Scenes/Ressource_items/Live_Scrap.tscn @@ -10,13 +10,14 @@ height = 4.0 [sub_resource type="CircleShape2D" id=2] radius = 51.0098 -[node name="Prey" type="RigidBody2D" groups=["Ressource"]] +[node name="Prey" type="RigidBody2D" groups=["Prey", "Ressource"]] z_index = 1 -collision_layer = 5 +collision_layer = 7 mode = 2 mass = 70.0 linear_damp = 2.0 script = ExtResource( 1 ) +_flags = [ "Scrap" ] [node name="Sprite" type="Sprite" parent="."] texture = ExtResource( 2 ) diff --git a/assets/Scenes/World.tscn b/assets/Scenes/World.tscn index fd4ab7e..8028442 100644 --- a/assets/Scenes/World.tscn +++ b/assets/Scenes/World.tscn @@ -289,6 +289,7 @@ points = PoolVector2Array( 0, 0, 24, 0, 24, 24, 0, 24 ) script = ExtResource( 3 ) ressource_fuel = ExtResource( 2 ) ressource_scrap = ExtResource( 8 ) +ressource_live_scrap = ExtResource( 9 ) [node name="TileMap" type="TileMap" parent="."] position = Vector2( -1, -1 ) diff --git a/src/AI_core.gd b/src/AI_core.gd index 4f2e83a..691c77a 100644 --- a/src/AI_core.gd +++ b/src/AI_core.gd @@ -48,6 +48,11 @@ func _on_Ressource_depot_body_entered(body: RigidBody2D) -> void: actor.release_grab() actor.new_path() + # Turn off Preys + if body.is_in_group("Prey"): + body.set_process(false) + body.set_physics_process(false) + #Disable collision with Actors body.set_collision_layer_bit(0,false) body.set_collision_mask_bit(0,false) diff --git a/src/Prey.gd b/src/Prey.gd index 15ba0a5..d93998d 100644 --- a/src/Prey.gd +++ b/src/Prey.gd @@ -56,7 +56,7 @@ func pick_fleeing_heading()->void: var chaser_vector:= Vector2.ZERO for body in _bodies_fleeing_from: chaser_vector += position.direction_to(body.position) - _heading = acos( - chaser_vector.x ) + _heading = acos( - chaser_vector.normalized().x ) func new_path()->void: pick_fleeing_heading() diff --git a/src/Robot.gd b/src/Robot.gd index ba2858a..ad6dc10 100644 --- a/src/Robot.gd +++ b/src/Robot.gd @@ -118,7 +118,7 @@ func pick_destination()->Vector2: func get_closest_ressource_in_awarness()->Body: var closest_body: Body for body in awarness_area.get_overlapping_bodies(): - if !_home_depot_area.overlaps_body(body) && !body.is_grabbed(): + if !_home_depot_area.overlaps_body(body) && ( !body.is_grabbed() || body.is_in_group("Prey")): if body.is_in_group("Ressource"): closest_body= body return closest_body diff --git a/src/World.gd b/src/World.gd index e44df2f..83a19a1 100644 --- a/src/World.gd +++ b/src/World.gd @@ -11,6 +11,7 @@ var MAX_X: = 6400.0 export var ressource_fuel: PackedScene export var ressource_scrap: PackedScene +export var ressource_live_scrap: PackedScene onready var timer:= $Timer @@ -48,7 +49,11 @@ func _add_ressource()-> void: if coin_flip > 1: new_ressource = ressource_fuel.instance() else: - new_ressource = ressource_scrap.instance() + coin_flip = randi() % 2 + 1 + if coin_flip > 1: + new_ressource = ressource_scrap.instance() + else: + new_ressource = ressource_live_scrap.instance() var rand_x = rand_range(MIN_X,MAX_X) var rand_Y = rand_range(MIN_Y,MAX_Y)