aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2022-10-11-handling-massive-worlds-in-godot.md
diff options
context:
space:
mode:
Diffstat (limited to 'content/posts/2022-10-11-handling-massive-worlds-in-godot.md')
-rw-r--r--content/posts/2022-10-11-handling-massive-worlds-in-godot.md82
1 files changed, 0 insertions, 82 deletions
diff --git a/content/posts/2022-10-11-handling-massive-worlds-in-godot.md b/content/posts/2022-10-11-handling-massive-worlds-in-godot.md
deleted file mode 100644
index 0fa30e8..0000000
--- a/content/posts/2022-10-11-handling-massive-worlds-in-godot.md
+++ /dev/null
@@ -1,82 +0,0 @@
1---
2title: Handling big worlds in Godot by splitting and lazy loading chunks of it
3url: handling-big-worlds-in-godot.html
4date: 2022-10-11
5draft: true
6---
7
8Because these examples are exported from **Godot to WebAssembly** and the packaging produces **large files**, you will need to **click to lazy load them**
9
10I have seen a couple of examples on the net, but never really a comprehensive guide how this would be achieved. My solution is nowhere perfect, but it will get you started. This code is also not optimized, so buyer beware.
11
12For the sake of simplicity, I will keep the terrain plain. I have been working on a terrain generation tool in my spare time, but due to the other obligations, that project was put on the back burner. If you however interested in it you can check it out on my personal Git repository [https://git.mitjafelicijan.com/village-creator.git/](https://git.mitjafelicijan.com/village-creator.git/).
13
14![Village Creator](/assets/godot-dynamic-tile-loading/village-creator.png)
15
16It's using a node system to generate a terrain mesh, and it's utilizing cellular automata for special effects like sand erosion and wind erosion, etc. Example of using cellular automata rule in an image below.
17
18![Cellular automata](/assets/godot-dynamic-tile-loading/cellular-automata.png)
19
20As I mentioned previously, still far away from being useful. Always, enough about that. Let’s move on to the topic of this post.
21
22## Handling lazy loading in 2D world
23
24To simplify things, we will try doing in 2D world first. It makes for a simpler exercise and eliminates a lot of complexity that comes with additional axis.
25
26### Player movement
27
28First, we need to take care of player movement. I will not go into many details here, since there are plenty of good tutorials on this topic on the interwebs. Suffice it to say, I created a [KinematicBody2D](https://docs.godotengine.org/en/stable/classes/class_kinematicbody2d.html) node that gives us goodies like `move_and_slide` and added input maps for [move_up, move_down, move_right, move_left] movement.
29
30```gdscript
31# Player2D.gd
32
33extends KinematicBody2D
34
35# Exported variables.
36export var speed = 300
37
38# Utility variables for later use.
39# Can be ignored for now.
40export var player_scale = 1
41export var camera_zoom = 1
42
43# Internal variables.
44var velocity = Vector2.ZERO
45
46func _ready():
47 # Can be ignored for now.
48 $Model.scale = Vector2(player_scale, player_scale)
49 $Camera.zoom = Vector2(camera_zoom, camera_zoom)
50
51# Executes on every physics frame (60, 144, etc).
52# This game has vsync enabled.
53func _physics_process(delta):
54 handle_player_movement()
55
56# Handles player movement.
57func handle_player_movement():
58 velocity = Vector2.ZERO
59 if Input.is_action_pressed('move_right'):
60 velocity.x += 1
61 if Input.is_action_pressed('move_left'):
62 velocity.x -= 1
63 if Input.is_action_pressed('move_down'):
64 velocity.y += 1
65 if Input.is_action_pressed('move_up'):
66 velocity.y -= 1
67
68 # Make sure diagonal movement isn't faster.
69 # Therefore vector needs to be normalized.
70 velocity = velocity.normalized() * speed
71 velocity = move_and_slide(velocity)
72```
73
74[Read more about vector normalization.](https://www.fundza.com/vectors/normalize/)
75
76`$Model` is just a sprite loaded and positioned into the scene. For the background, I again loaded just a normal sprite and positioned it to center. This makes it easier to test if the movement is working properly. Background will be removed when we will start dynamically loading the world.
77
78<video src="/assets/godot-dynamic-tile-loading/2d-player-movement.webm" controls></video>
79
80Code for player movement can be [downloaded from my Git server](https://git.mitjafelicijan.com/big-worlds-godot.git/snapshot/big-worlds-godot-55bcad79c11bd67e8268925d7edbf571aa31e3bf.zip).
81
82<!--<div class="ll-iframe w-full h-80" data-src="/assets/godot-dynamic-tile-loading/example1/"></div>-->