Rework world map viewer

Instead of creating TextureRects, draw a full-window quad with appropriate UVs
Also add a minimap and keyboard scrolling
This commit is contained in:
Luke Hubmayer-Werner 2023-12-02 00:07:02 +10:30
parent 1534d4fad7
commit 9678501a8c
2 changed files with 116 additions and 45 deletions

View File

@ -1,62 +1,129 @@
extends Node2D extends Control
var pal_shader_mat := preload('res://palette_mat.tres') var pal_shader_mat := preload('res://palette_mat.tres')
var worldmap_shader_mat := preload('res://worldmap_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_images := []
var map_textures := [] 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: #func _create_palette_and_atlas_texrects() -> void:
for tileset in 3: # for tileset in 3:
# Debug the Atlases # # Debug the Atlases
var texrect = TextureRect.new() # var texrect = TextureRect.new()
texrect.rect_position.x = tileset * 256 * 2 # texrect.rect_position.x = tileset * 256 * 2
texrect.rect_scale *= 16 # texrect.rect_scale *= 16
texrect.texture = SpriteLoader.worldmap_palette_textures[tileset] # texrect.texture = SpriteLoader.worldmap_palette_textures[tileset]
map_texrects.append(texrect) # map_texrects.append(texrect)
add_child(texrect) # add_child(texrect)
texrect = TextureRect.new() # texrect = TextureRect.new()
texrect.rect_position.x = tileset * 256 * 2 # texrect.rect_position.x = tileset * 256 * 2
texrect.rect_position.y = 256 # texrect.rect_position.y = 256
texrect.rect_scale *= 4 # texrect.rect_scale *= 4
texrect.texture = SpriteLoader.worldmap_tile_atlas_textures[tileset] # texrect.texture = SpriteLoader.worldmap_tile_atlas_textures[tileset]
texrect.material = pal_shader_mat.duplicate() # texrect.material = pal_shader_mat.duplicate()
texrect.material.set_shader_param('palette', SpriteLoader.worldmap_palette_textures[tileset]) # texrect.material.set_shader_param('palette', SpriteLoader.worldmap_palette_textures[tileset])
map_texrects.append(texrect) # map_texrects.append(texrect)
add_child(texrect) # add_child(texrect)
texrect = TextureRect.new() # texrect = TextureRect.new()
texrect.rect_position.x = tileset * 256 * 2 # texrect.rect_position.x = tileset * 256 * 2
texrect.rect_position.y = 768 # texrect.rect_position.y = 768
texrect.rect_scale *= 4 # texrect.rect_scale *= 4
texrect.texture = SpriteLoader.worldmap_tile_atlas_textures[tileset] # texrect.texture = SpriteLoader.worldmap_tile_atlas_textures[tileset]
map_texrects.append(texrect) # map_texrects.append(texrect)
add_child(texrect) # add_child(texrect)
func _create_worldmap_texrects() -> void: const map_tilesets = [0, 1, 0, 2, 2]
var map_tilesets = [0, 1, 0, 2, 2] const waterfall_scrolls = [true, false, true, false, false]
for i in 1: const sea_scrolls = [true, true, true, false, false]
func _create_worldmap_textures() -> void:
for i in 5:
var tileset = map_tilesets[i] var tileset = map_tilesets[i]
var image = MapLoader.worldmaps[i].make_tile_map() var image = MapLoader.worldmaps[i].make_tile_map()
map_images.append(image) self.map_images.append(image)
var tex := SpriteLoader.texture_from_image(image) var tex := SpriteLoader.texture_from_image(image, Texture.FLAG_REPEAT)
map_textures.append(tex) self.map_textures.append(tex)
var texrect = TextureRect.new()
texrect.rect_scale *= 8 #8 # Needs to have at least 1 pixel per tile pixel func _set_map(id: int) -> void:
texrect.texture = tex if id < 0 or id >= len(map_images):
texrect.material = worldmap_shader_mat.duplicate() print_debug('Invalid map ID %d' % id)
texrect.material.set_shader_param('palette', SpriteLoader.worldmap_palette_textures[tileset]) return
texrect.material.set_shader_param('tile_atlas', SpriteLoader.worldmap_tile_atlas_textures[tileset]) self.current_map_id = id
map_texrects.append(texrect) var tileset = map_tilesets[id]
add_child(texrect) 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. # Called when the node enters the scene tree for the first time.
func _ready() -> void: func _ready() -> void:
# Only create this after MapLoader and SpriteLoader have loaded! # Only create this after MapLoader and SpriteLoader have loaded!
_create_worldmap_texrects() _create_worldmap_textures()
# _create_palette_and_atlas_texrects() # _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. # Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta: float) -> void: func _process(delta: float) -> void:
# pass 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

View File

@ -2,5 +2,9 @@
[ext_resource path="res://test/worldmap_system.gd" type="Script" id=1] [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 ) script = ExtResource( 1 )
[node name="tr_minimap" type="TextureRect" parent="."]
margin_right = 40.0
margin_bottom = 40.0