diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2022-06-03 21:13:19 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2022-06-03 21:13:19 -0600 |
commit | f922fe4669080d1633e0a345a3f8981867c9e841 (patch) | |
tree | 7c2c98bfca20fda37fe34ba99e2d3caa9a39ca02 /src/entities | |
parent | 898877f09808691a5e5d45850d27ae85f270db16 (diff) |
add working world gen, fixed food placing, minor refactoring
Diffstat (limited to 'src/entities')
-rw-r--r-- | src/entities/actors/snake/scenes/Body.tscn | 4 | ||||
-rw-r--r-- | src/entities/actors/snake/scenes/Head.tscn | 5 | ||||
-rw-r--r-- | src/entities/actors/snake/scenes/Snake.tscn | 9 | ||||
-rw-r--r-- | src/entities/actors/snake/scenes/Tail.tscn | 3 | ||||
-rw-r--r-- | src/entities/actors/snake/scripts/camera.gd | 9 | ||||
-rw-r--r-- | src/entities/actors/snake/scripts/generic_segment.gd | 7 | ||||
-rw-r--r-- | src/entities/actors/snake/scripts/head.gd | 2 | ||||
-rw-r--r-- | src/entities/actors/snake/scripts/snake.gd | 22 | ||||
-rw-r--r-- | src/entities/actors/snake/sprites/body.png | bin | 168 -> 136 bytes | |||
-rw-r--r-- | src/entities/actors/snake/sprites/head.png | bin | 188 -> 174 bytes | |||
-rw-r--r-- | src/entities/actors/snake/sprites/tail.png | bin | 174 -> 133 bytes | |||
-rw-r--r-- | src/entities/food/scenes/Food.tscn | 2 | ||||
-rw-r--r-- | src/entities/food/scripts/food.gd | 3 | ||||
-rw-r--r-- | src/entities/food/scripts/food_manager.gd | 43 | ||||
-rw-r--r-- | src/entities/food/sprites/apple.png | bin | 148 -> 135 bytes |
15 files changed, 79 insertions, 30 deletions
diff --git a/src/entities/actors/snake/scenes/Body.tscn b/src/entities/actors/snake/scenes/Body.tscn index 49694c0..7b5de58 100644 --- a/src/entities/actors/snake/scenes/Body.tscn +++ b/src/entities/actors/snake/scenes/Body.tscn @@ -4,8 +4,8 @@ [ext_resource path="res://entities/actors/snake/scripts/generic_segment.gd" type="Script" id=2] [sub_resource type="CapsuleShape2D" id=1] -radius = 4.99999 -height = 4.00002 +radius = 1.99999 +height = 2.00001 [node name="BodyPathFollow" type="PathFollow2D"] loop = false diff --git a/src/entities/actors/snake/scenes/Head.tscn b/src/entities/actors/snake/scenes/Head.tscn index d118fbf..336265b 100644 --- a/src/entities/actors/snake/scenes/Head.tscn +++ b/src/entities/actors/snake/scenes/Head.tscn @@ -3,8 +3,8 @@ [ext_resource path="res://entities/actors/snake/sprites/head.png" type="Texture" id=1] [ext_resource path="res://entities/actors/snake/scripts/head.gd" type="Script" id=2] -[sub_resource type="ConvexPolygonShape2D" id=1] -points = PoolVector2Array( -5, 0, 5, 0, 5, -3, 3, -4, 1, -5, -1, -5, -3, -4, -5, -3 ) +[sub_resource type="CircleShape2D" id=1] +radius = 2.0 [node name="Head" type="KinematicBody2D"] collision_mask = 262 @@ -14,4 +14,5 @@ script = ExtResource( 2 ) texture = ExtResource( 1 ) [node name="Collision" type="CollisionShape2D" parent="."] +position = Vector2( 0, -2 ) shape = SubResource( 1 ) diff --git a/src/entities/actors/snake/scenes/Snake.tscn b/src/entities/actors/snake/scenes/Snake.tscn index cb1eb2a..ea234c4 100644 --- a/src/entities/actors/snake/scenes/Snake.tscn +++ b/src/entities/actors/snake/scenes/Snake.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://entities/actors/snake/scenes/Head.tscn" type="PackedScene" id=1] [ext_resource path="res://entities/actors/snake/scenes/Body.tscn" type="PackedScene" id=2] [ext_resource path="res://entities/actors/snake/scripts/snake.gd" type="Script" id=3] [ext_resource path="res://entities/actors/snake/scenes/Tail.tscn" type="PackedScene" id=4] +[ext_resource path="res://entities/actors/snake/scripts/camera.gd" type="Script" id=5] [sub_resource type="Curve2D" id=1] _data = { @@ -15,7 +16,11 @@ script = ExtResource( 3 ) BODY_SEGMENT_NP = ExtResource( 2 ) TAIL_SEGMENT_NP = ExtResource( 4 ) +[node name="Head" parent="." instance=ExtResource( 1 )] + [node name="Path" type="Path2D" parent="."] curve = SubResource( 1 ) -[node name="Head" parent="." instance=ExtResource( 1 )] +[node name="Camera" type="Camera2D" parent="."] +current = true +script = ExtResource( 5 ) diff --git a/src/entities/actors/snake/scenes/Tail.tscn b/src/entities/actors/snake/scenes/Tail.tscn index 17e8ec5..adfe422 100644 --- a/src/entities/actors/snake/scenes/Tail.tscn +++ b/src/entities/actors/snake/scenes/Tail.tscn @@ -4,7 +4,7 @@ [ext_resource path="res://entities/actors/snake/scripts/generic_segment.gd" type="Script" id=2] [sub_resource type="CircleShape2D" id=1] -radius = 4.12311 +radius = 2.0 [node name="TailPathFollow" type="PathFollow2D"] loop = false @@ -20,4 +20,5 @@ collision_mask = 0 texture = ExtResource( 1 ) [node name="Collision" type="CollisionShape2D" parent="Tail"] +position = Vector2( 7.23998e-06, 2 ) shape = SubResource( 1 ) diff --git a/src/entities/actors/snake/scripts/camera.gd b/src/entities/actors/snake/scripts/camera.gd new file mode 100644 index 0000000..c230dad --- /dev/null +++ b/src/entities/actors/snake/scripts/camera.gd @@ -0,0 +1,9 @@ +extends Camera2D + +onready var _snake_head: KinematicBody2D = get_parent().get_node("Head") +var _snake_position: Vector2 + + +func _physics_process(delta: float) -> void: + _snake_position = _snake_head.global_position + global_position = global_position.linear_interpolate(_snake_position, delta) diff --git a/src/entities/actors/snake/scripts/generic_segment.gd b/src/entities/actors/snake/scripts/generic_segment.gd index e2db91d..d0bb944 100644 --- a/src/entities/actors/snake/scripts/generic_segment.gd +++ b/src/entities/actors/snake/scripts/generic_segment.gd @@ -6,6 +6,7 @@ onready var _segment: Area2D = get_child(0) func _ready() -> void: + Event.connect("snake_rotated", self, "_on_snake_rotated") _segment.connect("body_entered", self, "_on_body_entered") @@ -15,3 +16,9 @@ func _physics_process(delta: float) -> void: func _on_body_entered(body: Node) -> void: Event.emit_signal("snake_segment_body_entered", body) + + +func _on_snake_rotated() -> void: + # this is just random, i need to offset a tiny bit whenever the snake rotates + # so that the first body segmetn doesn't catch up with the head + offset -= Global.SNAKE_SPEED * pow(get_physics_process_delta_time(), 2)
\ No newline at end of file diff --git a/src/entities/actors/snake/scripts/head.gd b/src/entities/actors/snake/scripts/head.gd index 448802e..117f461 100644 --- a/src/entities/actors/snake/scripts/head.gd +++ b/src/entities/actors/snake/scripts/head.gd @@ -5,6 +5,7 @@ enum { RIGHT=1 } +var _initial_speed: float = Global.SNAKE_SPEED var velocity: Vector2 = Vector2.ZERO var _direction: Vector2 = Vector2.UP var _time_elapsed: float = 0.0 @@ -26,6 +27,7 @@ func _physics_process(delta: float) -> void: func _rotate_to(direction: int) -> void: rotate(deg2rad(direction * Global.SNAKE_ROT_SPEED * get_physics_process_delta_time())) _direction = _direction.rotated(deg2rad(direction * Global.SNAKE_ROT_SPEED * get_physics_process_delta_time())) + Event.emit_signal("snake_rotated") # using a timer is not recommended for < 0.01 diff --git a/src/entities/actors/snake/scripts/snake.gd b/src/entities/actors/snake/scripts/snake.gd index ead6254..01e09da 100644 --- a/src/entities/actors/snake/scripts/snake.gd +++ b/src/entities/actors/snake/scripts/snake.gd @@ -13,6 +13,7 @@ var tail_segment: PathFollow2D # didn't konw how to name this, basically holds the current path lenght # whenever the add body segment, and we use this stack to add body parts var body_segment_queue: Array +# var distance_to_first_segment: float func _ready(): @@ -23,21 +24,28 @@ func _ready(): Event.connect("snake_added_initial_segments", self, "_on_snake_added_initial_segments") Event.connect("food_eaten", self, "_on_food_eaten") + # need to always set a new curve when ready, so when restarting it's af resh curve + path.curve = Curve2D.new() func _physics_process(delta: float) -> void: - if body_segment_queue.size() != 0: + if body_segment_queue.size() != 0 and current_body_segments > max_body_initial_segments: _add_new_segment() + # if body_segment_stack.size() > 0: + # distance_to_first_segment = path.curve.get_baked_length() - body_segment_stack.front().offset + # if distance_to_first_segment >= Global.SNAKE_SEGMENT_SIZE: + # print(distance_to_first_segment) + func _input(event: InputEvent) -> void: if event.is_action_pressed("add_body_part"): _add_segment_to_queue() -func _draw() -> void: - if path.curve.get_baked_points().size() >= 2: - draw_polyline(path.curve.get_baked_points(), Color.aquamarine, 1, true) +# func _draw() -> void: +# if path.curve.get_baked_points().size() >= 2: +# draw_polyline(path.curve.get_baked_points(), Color.aquamarine, 1, true) func _add_new_segment() -> void: @@ -83,7 +91,7 @@ func _add_segment_to_queue() -> void: func _on_snake_path_new_point(coordinates: Vector2) -> void: path.curve.add_point(coordinates) # update call is to draw curve as there are new points to the path's curve - update() + # update() if current_body_segments < max_body_initial_segments: _add_initial_segment(BODY_SEGMENT_NP) @@ -101,5 +109,5 @@ func _on_snake_added_initial_segments() -> void: set_process_input(true) -func _on_food_eaten(type: int) -> void: - _add_segment_to_queue()
\ No newline at end of file +func _on_food_eaten(type: int, location: Vector2) -> void: + _add_segment_to_queue() diff --git a/src/entities/actors/snake/sprites/body.png b/src/entities/actors/snake/sprites/body.png Binary files differindex ebef3b4..d94ff94 100644 --- a/src/entities/actors/snake/sprites/body.png +++ b/src/entities/actors/snake/sprites/body.png diff --git a/src/entities/actors/snake/sprites/head.png b/src/entities/actors/snake/sprites/head.png Binary files differindex 525de45..0bca146 100644 --- a/src/entities/actors/snake/sprites/head.png +++ b/src/entities/actors/snake/sprites/head.png diff --git a/src/entities/actors/snake/sprites/tail.png b/src/entities/actors/snake/sprites/tail.png Binary files differindex ff78e1b..05abc6e 100644 --- a/src/entities/actors/snake/sprites/tail.png +++ b/src/entities/actors/snake/sprites/tail.png diff --git a/src/entities/food/scenes/Food.tscn b/src/entities/food/scenes/Food.tscn index bc330e2..7accbc6 100644 --- a/src/entities/food/scenes/Food.tscn +++ b/src/entities/food/scenes/Food.tscn @@ -3,7 +3,7 @@ [ext_resource path="res://entities/food/scripts/food.gd" type="Script" id=2] [sub_resource type="CircleShape2D" id=1] -radius = 7.0 +radius = 4.0 [node name="Food" type="Area2D"] collision_layer = 256 diff --git a/src/entities/food/scripts/food.gd b/src/entities/food/scripts/food.gd index 1689837..e8b0261 100644 --- a/src/entities/food/scripts/food.gd +++ b/src/entities/food/scripts/food.gd @@ -11,6 +11,7 @@ var _type_texture: Dictionary = { export(Type) var TYPE onready var _sprite: Sprite = $Sprite +var location: Vector2 func _ready(): @@ -19,5 +20,5 @@ func _ready(): func _on_body_entered(body: Node) -> void: - Event.emit_signal("food_eaten", TYPE) + Event.emit_signal("food_eaten", TYPE, location) queue_free()
\ No newline at end of file diff --git a/src/entities/food/scripts/food_manager.gd b/src/entities/food/scripts/food_manager.gd index 20772db..426677c 100644 --- a/src/entities/food/scripts/food_manager.gd +++ b/src/entities/food/scripts/food_manager.gd @@ -2,38 +2,53 @@ class_name FoodManager extends Node2D export(PackedScene) var FOOD: PackedScene +export(NodePath) var WORLD_GENERATOR_NP: NodePath +onready var world_generator: Node2D = get_node(WORLD_GENERATOR_NP) +onready var possible_food_locations: Array = world_generator.get_valid_map_coords() var max_apples: int = 10 -var current_apples: int = 0 +var current_food: Array = [] func _ready(): - Event.connect("food_eaten", self, "_on_food_eaten") randomize() + Event.connect("food_eaten", self, "_on_food_eaten") func _process(delta) -> void: - if current_apples < max_apples: + if current_food.size() < max_apples: _place_new_food() - current_apples += 1 func _place_new_food() -> void: var food: Area2D = FOOD.instance() Event.emit_signal("food_placing_new_food", food.TYPE) - var position: Vector2 = _get_random_pos() - food.global_position = position + var pos_loc: Array = _get_random_pos() + var pos: Vector2 = pos_loc[0] + var loc: Vector2 = pos_loc[1] + + food.global_position = pos + food.location = loc add_child(food) - Event.emit_signal("food_placed_new_food", food.TYPE) + current_food.append(loc) + Event.emit_signal("food_placed_new_food", food.TYPE, loc) + +func _get_random_pos() -> Array: + var found_valid_loc: bool = false + var index: int + var location: Vector2 -func _get_random_pos() -> Vector2: - var screen_size: Vector2 = get_viewport().get_visible_rect().size - var temp_x: float = randf() * screen_size.x - screen_size.x / 2.0 - var temp_y: float = randf() * screen_size.y - screen_size.y / 2.0 + while not found_valid_loc: + print("trying") + index = randi() % possible_food_locations.size() + location = possible_food_locations[index] + if current_food.find(location) == -1: + found_valid_loc = true - return Vector2(temp_x, temp_y) + return [world_generator.get_centered_world_position(location), location] -func _on_food_eaten(type: int) -> void: - current_apples -= 1
\ No newline at end of file +func _on_food_eaten(type: int, location: Vector2) -> void: + var index: int = current_food.find(location) + current_food.remove(index) diff --git a/src/entities/food/sprites/apple.png b/src/entities/food/sprites/apple.png Binary files differindex d4fd6ad..7111450 100644 --- a/src/entities/food/sprites/apple.png +++ b/src/entities/food/sprites/apple.png |