diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2024-03-01 04:13:22 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2024-03-01 04:13:22 -0600 |
commit | ab5cc111d51a8975f5f82e5a0b497b5f752acc5d (patch) | |
tree | a722ea0636b4d35b9c47120b877de32e96fdfc74 /live/blog/rss.xml | |
parent | e6df5eecddf5443824aba28d09563df8c725debd (diff) |
add final flappybird godot devlog entry
Diffstat (limited to 'live/blog/rss.xml')
-rw-r--r-- | live/blog/rss.xml | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/live/blog/rss.xml b/live/blog/rss.xml index c725869..775006f 100644 --- a/live/blog/rss.xml +++ b/live/blog/rss.xml @@ -23,6 +23,157 @@ <link>https://blog.luevano.xyz</link> </image> <item> + <title>Final improvements to the FlappyBird clone and Android support devlog 3</title> + <link>https://blog.luevano.xyz/g/flappybird_godot_devlog_3.html</link> + <guid isPermaLink="true">https://blog.luevano.xyz/g/flappybird_godot_devlog_3.html</guid> + <pubDate>Fri, 01 Mar 2024 10:00:57 GMT</pubDate> + <category>English</category> + <category>Gamedev</category> + <category>Gdscript</category> + <category>Godot</category> + <description>Notes on the final improvements to my FlappyBird clone made in Godot 4.x. Also details on the support for Android.</description> + <content:encoded><![CDATA[<p>Decided to conclude my FlappyBird journey with one last set of improvements, following up on devlogs <a href="https://blog.luevano.xyz/g/flappybird_godot_devlog_1.html">1</a> and <a href="https://blog.luevano.xyz/g/flappybird_godot_devlog_2.html">2</a>. Focusing on <strong>refactoring</strong>, <strong>better UI</strong>, <strong>sprite selection</strong> and Android support.</p> +<p>I missed some features that I really wanted to get in but I’m already tired of working on this toy project and already eager to move to another one. Most of the features I wanted to add are just <em>QoL</em> UI enhancements and extra buttons basically.</p> +<p>The source code can be found at <a href="https://github.com/luevano/flappybirdgodot">luevano/flappybirdgodot</a>. Playable at <a href="https://lorentzeus.itch.io/flappybirdgodot">itch.io</a>:</p> +<p style="text-align:center"><iframe src="https://itch.io/embed/1551015?dark=true" width="208" height="167" frameborder="0"><a href="https://lorentzeus.itch.io/flappybirdgodot">FlappyBirdGodot by Lorentzeus</a></iframe></p> + +<h2 id="table-of-contents">Table of contents<a class="headerlink" href="#table-of-contents" title="Permanent link">¶</a></h2> +<div class="toc"> +<ul> +<li><a href="#table-of-contents">Table of contents</a></li> +<li><a href="#refactoring">Refactoring</a></li> +<li><a href="#improvements">Improvements</a><ul> +<li><a href="#background-parallax">Background parallax</a></li> +<li><a href="#sprite-switcher">Sprite switcher</a></li> +<li><a href="#save-data">Save data</a></li> +</ul> +</li> +<li><a href="#android">Android</a></li> +<li><a href="#misc">Misc</a></li> +</ul> +</div> +<h2 id="refactoring">Refactoring<a class="headerlink" href="#refactoring" title="Permanent link">¶</a></h2> +<p>The first part for my refactor was to move everything out of the <code>src/</code> directory into the root directory of the git repository, organizing it a tiny bit better, personal preference from what I’ve learned so far. I also decided to place all the raw aseprite assets next to the imported one, this way its easier to make modifications and then save directly in the same directory. Also, a list of other refactoring done:</p> +<ul> +<li>The way I handled the gameplay means that I needed to make the camera, background and the (ceiling and tiles) “detectors” move along with the player, while restricting their movement in the <code>x</code> axis, really hacky. Instead, I did what I should’ve done from the beginning… just let the tiles move backwards and keep everything static with the player only moving up an down (as how I stated at the beginning of <a href="https://blog.luevano.xyz/g/flappybird_godot_devlog_1.html">FlappyBirdgodot devlog 1</a> but didn’t actually follow).</li> +<li>Moved the <code>set_process</code> methodology to their own scripts, instead of handling everything in <code>main.gd</code> while also taking advantage of how signals work now. Instead of doing:</li> +</ul> +<pre><code class="language-gdscript">func _ready(): + Event.game_pause.connect(_on_game_pause) + +func _on_game_pause(pause: bool): + set_process(pause) +</code></pre> +<p>Just connecting to <code>set_process</code> is enough:</p> +<pre><code class="language-gdscript">func _ready(): + Event.game_pause.connect(set_process) + # and when the signal doesn't send anything: + Event.game_start.connect(set_process.bind(true)) + Event.game_over.connect(set_process.bind(false)) +</code></pre> +<h2 id="improvements">Improvements<a class="headerlink" href="#improvements" title="Permanent link">¶</a></h2> +<h3 id="background-parallax">Background parallax<a class="headerlink" href="#background-parallax" title="Permanent link">¶</a></h3> +<p>First thing was to add a moving background functionality, by adding 2 of the same <code>Sprite2D</code>‘s one after another and everytime the first sprite moves out of the screen, position it right after the second sprite. Some sample code to accomplish this:</p> +<pre><code class="language-gdscript">func _ready(): + # Sprite2D and CompressedTexture2D nodes + background_orig.texture = background_texture + texture_size = background_orig.texture.get_size() + + backgrounds.append(background_orig.duplicate()) + backgrounds.append(background_orig.duplicate()) + backgrounds[1].position = background_orig.position + Vector2(texture_size.x, 0.0) + + add_child(backgrounds[0]) + add_child(backgrounds[1]) + background_orig.visible = false + +# ifirst (index first) it's a boolean value starting with false and +# its a hacky way of tracking the first sprites +# (the one closest to the left of the screen) in the array +func _process(delta: float): + for background in backgrounds: + background.move_local_x(- SPEED * delta) + + # moves the sprite that just exited the screen to the right of the upcoming sprite + if backgrounds[int(ifirst)].position.x <= - background_orig.position.x: + backgrounds[int(ifirst)].position.x = backgrounds[int(!ifirst)].position.x + texture_size.x + ifirst = !ifirst +</code></pre> +<p>Then I added background parallax by separating the background sprites in two: background and “foreground” (the buildings in the original sprites). And to move them separately just applied the same logic described above with 2 different speeds.</p> +<h3 id="sprite-switcher">Sprite switcher<a class="headerlink" href="#sprite-switcher" title="Permanent link">¶</a></h3> +<p>Also added a way to select between the bird sprites and the backgrounds, currently pretty primitive but functional. Accomplished this by holding textures in an exported array, then added a bit of logic to cycle between them (example for the background):</p> +<pre><code class="language-gdscript">func _get_new_sprite_index(index: int) -> int: + return clampi(index, 0, background_textures.size() - 1) + + +func _set_sprites_index(index: int) -> int: + var new_index: int = _get_new_sprite_index(index) + if new_index == itexture: + return new_index + for bg in backgrounds: + bg.texture = background_textures[new_index] + for fg in foregrounds: + fg.texture = foreground_textures[new_index] + itexture = new_index + return new_index +</code></pre> +<p>Then, in custom signals I just call <code>_set_sprites_index</code> with a <code>texture_index +/- 1</code>.</p> +<h3 id="save-data">Save data<a class="headerlink" href="#save-data" title="Permanent link">¶</a></h3> +<p>Moved from manual <code>ConfigFile</code> (which is an <code>.ini</code> file basically) to <code>Resource</code> which is easier to work with and faster to implement.</p> +<p>Accomplished by defining a new <code>data_resource.gd</code>:</p> +<pre><code class="language-gdscript">class_name DataResource +extends Resource + +@export var high_score: int +@export var volume: float +@export var mute: bool +@export var bird: int +@export var background: int + +func _init(): + high_score = 0 + volume = 0.5 + mute = false + bird = 0 + background = 0 +</code></pre> +<p>Where the <code>@export</code>s are not needed unless you need to manage the <code>.tres</code> resource files for testing in-editor.</p> +<p>Then, the <code>data.gd</code> script needs to be changed accordingly, most notably:</p> +<ul> +<li>The file extension is <code>.tres</code> instead of <code>.cfg</code>.</li> +<li>No need for “config sections”.</li> +<li>Saving changes to:</li> +</ul> +<pre><code class="language-gdscript">func save(): + var err: int = ResourceSaver.save(_data, DATA_PATH) + if err != OK: + print("[ERROR] Couldn't save data.") +</code></pre> +<ul> +<li>The loader gets simplified (mostly because the default values are set in the <code>_init()</code> function of the <code>data_resource.gd</code>) to:</li> +</ul> +<pre><code class="language-gdscript">func _load_data(): + if ResourceLoader.exists(DATA_PATH): + _data = load(DATA_PATH) + else: + _data = DataResource.new() + save() +</code></pre> +<ul> +<li>The attributes/config/saved data can be retrieved directly by the <code>data_resource.gd</code> variable name, for example: instead of <code>_data.get_value(SCORE_SECTION, "high_score")</code> it’s now simply <code>_data.high_score</code>. And similar for setting the values.</li> +</ul> +<p>Compared to the <a href="https://blog.luevano.xyz/g/flappybird_godot_devlog_1.html#saved-data">3.x version</a> it is a lot more simple. Though I still have setters and getters for each attribute/config (I’ll se how to change this in the future).</p> +<h2 id="android">Android<a class="headerlink" href="#android" title="Permanent link">¶</a></h2> +<p>I did add android support but it’s been so long since I did it that I actually don’t remember (this entry has been sitting in a draft for months). In general I followed the official guide for <a href="https://docs.godotengine.org/en/stable/tutorials/export/exporting_for_android.html">Exporting for Android</a>, setting up Android studio and remotely debugging with my personal phone; it does take a while to setup but after that it’s as simple as doing <em>“one click deploys”</em>.</p> +<p>Most notably, I had to enable touch screen support and make the buttons clickable either by an actual mouse click or touch input. Some of the <em>Project Settings</em> that I remember that needs changes are:</p> +<ul> +<li><em>display/window/handheld/orientation</em> set to <code>Portrait</code>.</li> +<li><em>input_devices/pointing/emulate_touch_from_mouse</em> and <em>input_devices/pointing/emulate_mouse_from_touch</em> both set to <code>on</code>.</li> +</ul> +<h2 id="misc">Misc<a class="headerlink" href="#misc" title="Permanent link">¶</a></h2> +<p>Found a bug on the <code>ScoreDetector</code> where it would collide with the <code>Ceiling</code>. While this is really not a problem outside of me doing tests I fixed it by <a href="https://blog.luevano.xyz/g/godot_layers_and_masks_notes.html">applying the correct layer/mask</a>.</p>]]></content:encoded> + </item> + <item> <title>Godot layers and masks notes</title> <link>https://blog.luevano.xyz/g/godot_layers_and_masks_notes.html</link> <guid isPermaLink="true">https://blog.luevano.xyz/g/godot_layers_and_masks_notes.html</guid> |