Skip to content

Commit

Permalink
Merge pull request #9 from Fris0uman/gears
Browse files Browse the repository at this point in the history
Divercify ressources
  • Loading branch information
Fris0uman authored Jul 30, 2023
2 parents 5e068d4 + a4fe1b0 commit 7ffb809
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 15 deletions.
23 changes: 23 additions & 0 deletions assets/Scenes/Ressource_items/Fuel.tscn
Original file line number Diff line number Diff line change
@@ -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 )
45 changes: 45 additions & 0 deletions assets/Scenes/Ressource_items/Live_Scrap.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[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=["Prey", "Ressource"]]
z_index = 1
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 )

[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="."]
24 changes: 24 additions & 0 deletions assets/Scenes/Ressource_items/Scrap.tscn
Original file line number Diff line number Diff line change
@@ -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 )
13 changes: 10 additions & 3 deletions assets/Scenes/World.tscn
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[gd_scene load_steps=32 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/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]
[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 )
Expand Down Expand Up @@ -285,7 +287,9 @@ 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 )
ressource_live_scrap = ExtResource( 9 )

[node name="TileMap" type="TileMap" parent="."]
position = Vector2( -1, -1 )
Expand All @@ -312,4 +316,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"]
Binary file added assets/cdda_sprites/e_scrap_season_winter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions assets/cdda_sprites/e_scrap_season_winter.png.import
Original file line number Diff line number Diff line change
@@ -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
28 changes: 19 additions & 9 deletions src/AI_core.gd
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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:
Expand All @@ -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
Expand All @@ -43,13 +48,18 @@ 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)

body.set_modulate(Color.dimgray)

_ressources.append(body)
sort_ressource(body)

func make_new_bot()->void:
var new_robot = robot_template.instance()
Expand All @@ -58,7 +68,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
4 changes: 4 additions & 0 deletions src/Body.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -45,3 +46,6 @@ func is_grabbed()->bool:

func get_grabbers()->Array:
return _grabbers

func has_flag(flag: String)->bool:
return _flags.has(flag)
91 changes: 91 additions & 0 deletions src/Prey.gd
Original file line number Diff line number Diff line change
@@ -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.normalized().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
2 changes: 1 addition & 1 deletion src/Robot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions src/World.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ 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
export var ressource_live_scrap: PackedScene

onready var timer:= $Timer

Expand Down Expand Up @@ -42,7 +44,17 @@ 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:
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)
new_ressource.position = Vector2(rand_x,rand_Y)
Expand Down

0 comments on commit 7ffb809

Please sign in to comment.