diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2022-06-02 02:44:18 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2022-06-02 02:44:18 -0600 |
commit | 65be853fff43b8b0203856fe98b64c190fd271f2 (patch) | |
tree | 510f02ccdbb36e267fc182de526866adf314bd4c | |
parent | 683f41b6e0873581a039cc4b2740b74745710461 (diff) |
update gogodot jam3 entry with food system
-rw-r--r-- | blog/dst/g/gogodot_jam3_devlog_1.html | 62 | ||||
-rw-r--r-- | blog/dst/index.html | 2 | ||||
-rw-r--r-- | blog/dst/tag/@english.html | 2 | ||||
-rw-r--r-- | blog/dst/tag/@gamedev.html | 2 | ||||
-rw-r--r-- | blog/dst/tag/@gamejam.html | 2 | ||||
-rw-r--r-- | blog/dst/tag/@godot.html | 2 | ||||
-rw-r--r-- | blog/src/g/gogodot_jam3_devlog_1.md | 74 | ||||
-rwxr-xr-x | static/images/g/gogodot_jam3/snake_food_basic_interaction.gif | bin | 0 -> 385400 bytes |
8 files changed, 136 insertions, 10 deletions
diff --git a/blog/dst/g/gogodot_jam3_devlog_1.html b/blog/dst/g/gogodot_jam3_devlog_1.html index 9d6e945..92480c4 100644 --- a/blog/dst/g/gogodot_jam3_devlog_1.html +++ b/blog/dst/g/gogodot_jam3_devlog_1.html @@ -216,7 +216,7 @@ func _on_Head_snake_path_new_point(coordinates: Vector2) -> void: <img alt="Snake - Basic movement with all body parts" src="images/g/gogodot_jam3/snake_basic_movement_added_body_parts.gif" title="Snake - Basic movement with all body parts"> <figcaption>Snake - Basic movement with all body parts</figcaption> </figure> -<p>Now, we need to handle adding body parts after the snake is complete and already moved for a bit, this will require a queue so we can add part by part in the case that we eat multiple pieces of food in a short period of time. For this we need to add some signals: <code>snake_add_new_segment(type)</code>, <code>snake_added_new_segment(type)</code>, <code>snake_added_initial_segments</code> and use them when makes sense. Now we need to add the following:</p> +<p>Now, we need to handle adding body parts after the snake is complete and already moved for a bit, this will require a queue so we can add part by part in the case that we eat multiple pieces of food in a short period of time. For this we need to add some signals: <code>snake_adding_new_segment(type)</code>, <code>snake_added_new_segment(type)</code>, <code>snake_added_initial_segments</code> and use them when makes sense. Now we need to add the following:</p> <pre><code class="language-gdscript">var body_segment_stack: Array var tail_segment: PathFollow2D # didn't konw how to name this, basically holds the current path lenght @@ -259,7 +259,63 @@ func _add_segment_to_queue() -> void: <img alt="Snake - Basic movement with dynamic addition of new segments" src="images/g/gogodot_jam3/snake_basic_movement_with_dynamic_segments.gif" title="Snake - Basic movement with dynamic addition of new segments"> <figcaption>Snake - Basic movement with dynamic addition of new segments</figcaption> </figure> -<p>For now, this should be enough, I’ll add more stuff as needed as I go.</p> +<p>For now, this should be enough, I’ll add more stuff as needed as I go. Last thing is that after finished testing that the movement felt ok, I just added a way to stop the snake whenever it collides with itself by using the following code (and the signal <code>snake_segment_body_entered(body)</code>) in a <code>main.gd</code> script that is the entry point for the game:</p> +<pre><code class="language-gdscript">func _snake_disabled(on_off: bool) -> void: + _snake.propagate_call("set_process", [on_off]) + _snake.propagate_call("set_process_internal", [on_off]) + _snake.propagate_call("set_physics_process", [on_off]) + _snake.propagate_call("set_physics_process_internal", [on_off]) + _snake.propagate_call("set_process_input", [on_off]) +</code></pre> +<p>Which will stop the snake node and all children.</p> +<h2 id="the-food">The food</h2> +<p>For now I just decided to setup a simple system to see everything works fine. The idea is to make some kind of generic food node/scene and a “food manager” to spawn them, for now in totally random locations. For this I added the following signals: <code>food_placing_new_food(type)</code>, <code>food_placed_new_food(type)</code> and <code>food_eaten(type)</code>.</p> +<p>First thing is creating the <code>Food.tscn</code> which is just an <em>Area2D</em> with its necessary children with an attached script called <code>food.gd</code>. The script is really simple:</p> +<pre><code class="language-gdscript">class_name Food # needed to access Type enum outside of the script, this registers this script as a node +extends Area2D + +enum Type { + APPLE +} + +var _type_texture: Dictionary = { + Type.APPLE: preload("res://entities/food/sprites/apple.png") +} + +export(Type) var TYPE +onready var _sprite: Sprite = $Sprite + + +func _ready(): + connect("body_entered", self, "_on_body_entered") + _sprite.texture = _type_texture[TYPE] + + +func _on_body_entered(body: Node) -> void: + Event.emit_signal("food_eaten", TYPE) + queue_free() +</code></pre> +<p>Then this <code>food_eaten</code> signal is received in <code>snake.gd</code> to add a new segment to the queue.</p> +<p>Finally, for the food manager I just created a <code>FoodManager.tscn</code> with a <em>Node2D</em> with an attached script called <code>food_manager.gd</code>. To get a random position:</p> +<pre><code class="language-gdscript">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 + + return Vector2(temp_x, temp_y) +</code></pre> +<p>Which gets the job done, but later I’ll have to add a way to check that the position is valid. And to actually place the food:</p> +<pre><code class="language-gdscript">func _place_new_food() -> void: + var food: Area2D = FOOD.instance() + var position: Vector2 = _get_random_pos() + food.global_position = position + add_child(food) +</code></pre> +<p>And this is used in <code>_process</code> to place new food whenever needed. For now I added a condition to add food until 10 pieces are in place, and keep adding whenever the food is is lower than 10. After setting everything up, this is the result:</p> +<figure id="__yafg-figure-9"> +<img alt="Snake - Food basic interaction" src="images/g/gogodot_jam3/snake_food_basic_interaction.gif" title="Snake - Food basic interaction"> +<figcaption>Snake - Food basic interaction</figcaption> +</figure> <h2 id="brainstormto-do">Brainstorm/To-do</h2> <ul> <li> @@ -325,7 +381,7 @@ func _add_segment_to_queue() -> void: <hr> <div class="article-info"> <p>By David LuĂ©vano</p> - <p>Created: Wed, Jun 01, 2022 @ 09:21 UTC</p> + <p>Created: Thu, Jun 02, 2022 @ 08:34 UTC</p> <div class="article-tags"> <p>Tags: <a href="https://blog.luevano.xyz/tag/@english.html">english</a>, <a href="https://blog.luevano.xyz/tag/@gamedev.html">gamedev</a>, <a href="https://blog.luevano.xyz/tag/@gamejam.html">gamejam</a>, <a href="https://blog.luevano.xyz/tag/@godot.html">godot</a> </p> diff --git a/blog/dst/index.html b/blog/dst/index.html index e2c0d33..d2f2da6 100644 --- a/blog/dst/index.html +++ b/blog/dst/index.html @@ -95,7 +95,7 @@ <h2>Articles</h2> <ul class="page-list"> <h3>June 2022</h3> - <li>Jun 01 - <a href="https://blog.luevano.xyz/g/gogodot_jam3_devlog_1.html">Creating my Go Godot Jam 3 entry devlog 1</a></li> + <li>Jun 02 - <a href="https://blog.luevano.xyz/g/gogodot_jam3_devlog_1.html">Creating my Go Godot Jam 3 entry devlog 1</a></li> <h3>May 2022</h3> <li>May 29 - <a href="https://blog.luevano.xyz/g/flappybird_godot_devlog_1.html">Creating a FlappyBird clone in Godot 3.5 devlog 1</a></li> <li>May 22 - <a href="https://blog.luevano.xyz/g/godot_project_structure.html">General Godot project structure</a></li> diff --git a/blog/dst/tag/@english.html b/blog/dst/tag/@english.html index c250639..3fab489 100644 --- a/blog/dst/tag/@english.html +++ b/blog/dst/tag/@english.html @@ -81,7 +81,7 @@ <h2>Articles</h2> <ul class="page-list"> <h3>June 2022</h3> - <li>Jun 01 - <a href="https://blog.luevano.xyz/g/gogodot_jam3_devlog_1.html">Creating my Go Godot Jam 3 entry devlog 1</a></li> + <li>Jun 02 - <a href="https://blog.luevano.xyz/g/gogodot_jam3_devlog_1.html">Creating my Go Godot Jam 3 entry devlog 1</a></li> <h3>May 2022</h3> <li>May 29 - <a href="https://blog.luevano.xyz/g/flappybird_godot_devlog_1.html">Creating a FlappyBird clone in Godot 3.5 devlog 1</a></li> <li>May 22 - <a href="https://blog.luevano.xyz/g/godot_project_structure.html">General Godot project structure</a></li> diff --git a/blog/dst/tag/@gamedev.html b/blog/dst/tag/@gamedev.html index b8640e4..ec5f50d 100644 --- a/blog/dst/tag/@gamedev.html +++ b/blog/dst/tag/@gamedev.html @@ -81,7 +81,7 @@ <h2>Articles</h2> <ul class="page-list"> <h3>June 2022</h3> - <li>Jun 01 - <a href="https://blog.luevano.xyz/g/gogodot_jam3_devlog_1.html">Creating my Go Godot Jam 3 entry devlog 1</a></li> + <li>Jun 02 - <a href="https://blog.luevano.xyz/g/gogodot_jam3_devlog_1.html">Creating my Go Godot Jam 3 entry devlog 1</a></li> <h3>May 2022</h3> <li>May 29 - <a href="https://blog.luevano.xyz/g/flappybird_godot_devlog_1.html">Creating a FlappyBird clone in Godot 3.5 devlog 1</a></li> <li>May 22 - <a href="https://blog.luevano.xyz/g/godot_project_structure.html">General Godot project structure</a></li> diff --git a/blog/dst/tag/@gamejam.html b/blog/dst/tag/@gamejam.html index 7638ffb..b6a4f0d 100644 --- a/blog/dst/tag/@gamejam.html +++ b/blog/dst/tag/@gamejam.html @@ -81,7 +81,7 @@ <h2>Articles</h2> <ul class="page-list"> <h3>June 2022</h3> - <li>Jun 01 - <a href="https://blog.luevano.xyz/g/gogodot_jam3_devlog_1.html">Creating my Go Godot Jam 3 entry devlog 1</a></li> + <li>Jun 02 - <a href="https://blog.luevano.xyz/g/gogodot_jam3_devlog_1.html">Creating my Go Godot Jam 3 entry devlog 1</a></li> </ul> diff --git a/blog/dst/tag/@godot.html b/blog/dst/tag/@godot.html index 70023e4..cce39be 100644 --- a/blog/dst/tag/@godot.html +++ b/blog/dst/tag/@godot.html @@ -81,7 +81,7 @@ <h2>Articles</h2> <ul class="page-list"> <h3>June 2022</h3> - <li>Jun 01 - <a href="https://blog.luevano.xyz/g/gogodot_jam3_devlog_1.html">Creating my Go Godot Jam 3 entry devlog 1</a></li> + <li>Jun 02 - <a href="https://blog.luevano.xyz/g/gogodot_jam3_devlog_1.html">Creating my Go Godot Jam 3 entry devlog 1</a></li> <h3>May 2022</h3> <li>May 29 - <a href="https://blog.luevano.xyz/g/flappybird_godot_devlog_1.html">Creating a FlappyBird clone in Godot 3.5 devlog 1</a></li> </ul> diff --git a/blog/src/g/gogodot_jam3_devlog_1.md b/blog/src/g/gogodot_jam3_devlog_1.md index edd4868..0c177c4 100644 --- a/blog/src/g/gogodot_jam3_devlog_1.md +++ b/blog/src/g/gogodot_jam3_devlog_1.md @@ -170,7 +170,7 @@ Select the *Snake* node and add the *Body* and *Tail* scene to the parameters, r ![Snake - Basic movement with all body parts](images/g/gogodot_jam3/snake_basic_movement_added_body_parts.gif "Snake - Basic movement with all body parts") -Now, we need to handle adding body parts after the snake is complete and already moved for a bit, this will require a queue so we can add part by part in the case that we eat multiple pieces of food in a short period of time. For this we need to add some signals: `snake_add_new_segment(type)`, `snake_added_new_segment(type)`, `snake_added_initial_segments` and use them when makes sense. Now we need to add the following: +Now, we need to handle adding body parts after the snake is complete and already moved for a bit, this will require a queue so we can add part by part in the case that we eat multiple pieces of food in a short period of time. For this we need to add some signals: `snake_adding_new_segment(type)`, `snake_added_new_segment(type)`, `snake_added_initial_segments` and use them when makes sense. Now we need to add the following: ```gdscript var body_segment_stack: Array @@ -221,7 +221,77 @@ With everything implemented and connected accordingly then we can add segments o ![Snake - Basic movement with dynamic addition of new segments](images/g/gogodot_jam3/snake_basic_movement_with_dynamic_segments.gif "Snake - Basic movement with dynamic addition of new segments") -For now, this should be enough, I'll add more stuff as needed as I go. +For now, this should be enough, I'll add more stuff as needed as I go. Last thing is that after finished testing that the movement felt ok, I just added a way to stop the snake whenever it collides with itself by using the following code (and the signal `snake_segment_body_entered(body)`) in a `main.gd` script that is the entry point for the game: + +```gdscript +func _snake_disabled(on_off: bool) -> void: + _snake.propagate_call("set_process", [on_off]) + _snake.propagate_call("set_process_internal", [on_off]) + _snake.propagate_call("set_physics_process", [on_off]) + _snake.propagate_call("set_physics_process_internal", [on_off]) + _snake.propagate_call("set_process_input", [on_off]) +``` + +Which will stop the snake node and all children. + +## The food + +For now I just decided to setup a simple system to see everything works fine. The idea is to make some kind of generic food node/scene and a "food manager" to spawn them, for now in totally random locations. For this I added the following signals: `food_placing_new_food(type)`, `food_placed_new_food(type)` and `food_eaten(type)`. + +First thing is creating the `Food.tscn` which is just an *Area2D* with its necessary children with an attached script called `food.gd`. The script is really simple: + +```gdscript +class_name Food # needed to access Type enum outside of the script, this registers this script as a node +extends Area2D + +enum Type { + APPLE +} + +var _type_texture: Dictionary = { + Type.APPLE: preload("res://entities/food/sprites/apple.png") +} + +export(Type) var TYPE +onready var _sprite: Sprite = $Sprite + + +func _ready(): + connect("body_entered", self, "_on_body_entered") + _sprite.texture = _type_texture[TYPE] + + +func _on_body_entered(body: Node) -> void: + Event.emit_signal("food_eaten", TYPE) + queue_free() +``` + +Then this `food_eaten` signal is received in `snake.gd` to add a new segment to the queue. + +Finally, for the food manager I just created a `FoodManager.tscn` with a *Node2D* with an attached script called `food_manager.gd`. To get a random position: + +```gdscript +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 + + return Vector2(temp_x, temp_y) +``` + +Which gets the job done, but later I'll have to add a way to check that the position is valid. And to actually place the food: + +```gdscript +func _place_new_food() -> void: + var food: Area2D = FOOD.instance() + var position: Vector2 = _get_random_pos() + food.global_position = position + add_child(food) +``` + +And this is used in `_process` to place new food whenever needed. For now I added a condition to add food until 10 pieces are in place, and keep adding whenever the food is is lower than 10. After setting everything up, this is the result: + +![Snake - Food basic interaction](images/g/gogodot_jam3/snake_food_basic_interaction.gif "Snake - Food basic interaction") ## Brainstorm/To-do diff --git a/static/images/g/gogodot_jam3/snake_food_basic_interaction.gif b/static/images/g/gogodot_jam3/snake_food_basic_interaction.gif Binary files differnew file mode 100755 index 0000000..6d7ec18 --- /dev/null +++ b/static/images/g/gogodot_jam3/snake_food_basic_interaction.gif |