From 9678501a8c2ef736e0644c01ade61ee146d2b40e Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Sat, 2 Dec 2023 00:07:02 +1030 Subject: [PATCH] Rework world map viewer Instead of creating TextureRects, draw a full-window quad with appropriate UVs Also add a minimap and keyboard scrolling --- test/worldmap_system.gd | 155 +++++++++++++++++++++++++++----------- test/worldmap_system.tscn | 6 +- 2 files changed, 116 insertions(+), 45 deletions(-) diff --git a/test/worldmap_system.gd b/test/worldmap_system.gd index e4bb408..e82664d 100644 --- a/test/worldmap_system.gd +++ b/test/worldmap_system.gd @@ -1,62 +1,129 @@ -extends Node2D +extends Control var pal_shader_mat := preload('res://palette_mat.tres') var worldmap_shader_mat := preload('res://worldmap_palette_mat.tres') +onready var minimap := $tr_minimap +var current_map_id := 0 var map_images := [] var map_textures := [] -var map_texrects := [] +var current_texture: Texture +var minimap_mode := 0 +var minimap_tween := 0.0 -func _create_palette_and_atlas_texrects() -> void: - for tileset in 3: - # Debug the Atlases - var texrect = TextureRect.new() - texrect.rect_position.x = tileset * 256 * 2 - texrect.rect_scale *= 16 - texrect.texture = SpriteLoader.worldmap_palette_textures[tileset] - map_texrects.append(texrect) - add_child(texrect) - texrect = TextureRect.new() - texrect.rect_position.x = tileset * 256 * 2 - texrect.rect_position.y = 256 - texrect.rect_scale *= 4 - texrect.texture = SpriteLoader.worldmap_tile_atlas_textures[tileset] - texrect.material = pal_shader_mat.duplicate() - texrect.material.set_shader_param('palette', SpriteLoader.worldmap_palette_textures[tileset]) - map_texrects.append(texrect) - add_child(texrect) - texrect = TextureRect.new() - texrect.rect_position.x = tileset * 256 * 2 - texrect.rect_position.y = 768 - texrect.rect_scale *= 4 - texrect.texture = SpriteLoader.worldmap_tile_atlas_textures[tileset] - map_texrects.append(texrect) - add_child(texrect) +#func _create_palette_and_atlas_texrects() -> void: +# for tileset in 3: +# # Debug the Atlases +# var texrect = TextureRect.new() +# texrect.rect_position.x = tileset * 256 * 2 +# texrect.rect_scale *= 16 +# texrect.texture = SpriteLoader.worldmap_palette_textures[tileset] +# map_texrects.append(texrect) +# add_child(texrect) +# texrect = TextureRect.new() +# texrect.rect_position.x = tileset * 256 * 2 +# texrect.rect_position.y = 256 +# texrect.rect_scale *= 4 +# texrect.texture = SpriteLoader.worldmap_tile_atlas_textures[tileset] +# texrect.material = pal_shader_mat.duplicate() +# texrect.material.set_shader_param('palette', SpriteLoader.worldmap_palette_textures[tileset]) +# map_texrects.append(texrect) +# add_child(texrect) +# texrect = TextureRect.new() +# texrect.rect_position.x = tileset * 256 * 2 +# texrect.rect_position.y = 768 +# texrect.rect_scale *= 4 +# texrect.texture = SpriteLoader.worldmap_tile_atlas_textures[tileset] +# map_texrects.append(texrect) +# add_child(texrect) -func _create_worldmap_texrects() -> void: - var map_tilesets = [0, 1, 0, 2, 2] - for i in 1: +const map_tilesets = [0, 1, 0, 2, 2] +const waterfall_scrolls = [true, false, true, false, false] +const sea_scrolls = [true, true, true, false, false] +func _create_worldmap_textures() -> void: + for i in 5: var tileset = map_tilesets[i] var image = MapLoader.worldmaps[i].make_tile_map() - map_images.append(image) - var tex := SpriteLoader.texture_from_image(image) - map_textures.append(tex) - var texrect = TextureRect.new() - texrect.rect_scale *= 8 #8 # Needs to have at least 1 pixel per tile pixel - texrect.texture = tex - texrect.material = worldmap_shader_mat.duplicate() - texrect.material.set_shader_param('palette', SpriteLoader.worldmap_palette_textures[tileset]) - texrect.material.set_shader_param('tile_atlas', SpriteLoader.worldmap_tile_atlas_textures[tileset]) - map_texrects.append(texrect) - add_child(texrect) + self.map_images.append(image) + var tex := SpriteLoader.texture_from_image(image, Texture.FLAG_REPEAT) + self.map_textures.append(tex) + +func _set_map(id: int) -> void: + if id < 0 or id >= len(map_images): + print_debug('Invalid map ID %d' % id) + return + self.current_map_id = id + var tileset = map_tilesets[id] + self.current_texture = self.map_textures[self.current_map_id] + self.material.set_shader_param('palette', SpriteLoader.worldmap_palette_textures[tileset]) + self.material.set_shader_param('tile_atlas', SpriteLoader.worldmap_tile_atlas_textures[tileset]) + self.material.set_shader_param('enable_waterfall_scroll', waterfall_scrolls[id]) + self.material.set_shader_param('enable_sea_scroll', sea_scrolls[id]) + minimap.texture = self.current_texture + minimap.material.set_shader_param('palette', SpriteLoader.worldmap_palette_textures[tileset]) + minimap.material.set_shader_param('tile_atlas', SpriteLoader.worldmap_tile_atlas_textures[tileset]) + minimap.material.set_shader_param('enable_waterfall_scroll', false) + minimap.material.set_shader_param('enable_sea_scroll', false) # Called when the node enters the scene tree for the first time. func _ready() -> void: # Only create this after MapLoader and SpriteLoader have loaded! - _create_worldmap_texrects() + _create_worldmap_textures() # _create_palette_and_atlas_texrects() + self.material = worldmap_shader_mat.duplicate() + minimap.material = worldmap_shader_mat.duplicate() + minimap.modulate.a8 = 0xc0 + minimap.rect_scale = Vector2(0.125, 0.125) + minimap.rect_position = Vector2(8, 168) + _set_map(0) # Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta: float) -> void: -# pass +func _process(delta: float) -> void: + self.pos = self.pos.posmod(256.0) + self.minimap_tween -= sign(minimap_tween - minimap_mode) * delta #* 0.06125 + self.minimap_tween = clamp(minimap_tween, 0, 1) + var l = ease(minimap_tween, -3.75) +# l = minimap_tween + minimap.rect_scale = Vector2.ONE * lerp(0.125, 0.5, l) + minimap.rect_position = Vector2(lerp(8, 64, l), lerp(168, -8, l)) + minimap.modulate.a8 = lerp(192, 240, l) + update() + +var pos := Vector2(0, 0) +const VW := 48 +const VH := 30 +const UV_W2 := VW/1024.0 +const UV_H2 := VH/1024.0 +const V_POINTS := PoolVector2Array([Vector2(0, 0), Vector2(384, 0), Vector2(384, 240), Vector2(0, 240)]) +const V_COLORS := PoolColorArray() +func _draw() -> void: + var uvpos := pos/256.0 + var v_uvs := PoolVector2Array([uvpos+Vector2(-UV_W2, -UV_H2), uvpos+Vector2(UV_W2, -UV_H2), uvpos+Vector2(UV_W2, UV_H2), uvpos+Vector2(-UV_W2, UV_H2)]) + draw_primitive(V_POINTS, V_COLORS, v_uvs, self.current_texture) + #draw_texture_rect_region(self.map_textures[0], Rect2(0, 0, 384, 240), Rect2(pos.x, pos.y, pos.x+VW, pos.y+VH), Color.white, false, null, false) + +func _input(event: InputEvent) -> void: + if event is InputEventKey: + if event.pressed: + match event.physical_scancode: + KEY_RIGHT: + pos.x += 1 + KEY_LEFT: + pos.x -= 1 + KEY_DOWN: + pos.y += 1 + KEY_UP: + pos.y -= 1 + KEY_1: + _set_map(0) + KEY_2: + _set_map(1) + KEY_3: + _set_map(2) + KEY_4: + _set_map(3) + KEY_5: + _set_map(4) + KEY_0: + self.minimap_mode = 1 - minimap_mode diff --git a/test/worldmap_system.tscn b/test/worldmap_system.tscn index 05b7f2d..8e88937 100644 --- a/test/worldmap_system.tscn +++ b/test/worldmap_system.tscn @@ -2,5 +2,9 @@ [ext_resource path="res://test/worldmap_system.gd" type="Script" id=1] -[node name="worldmap_system" type="Node2D"] +[node name="worldmap_system" type="Control"] script = ExtResource( 1 ) + +[node name="tr_minimap" type="TextureRect" parent="."] +margin_right = 40.0 +margin_bottom = 40.0