ChocolateBird/test/worldmap_system.gd

179 lines
6.8 KiB
GDScript3
Raw Normal View History

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
2024-07-05 02:11:24 +09:30
var map_images := [null, null, null, null, null]
var map_textures := [null, null, null, null, null]
var map_regional_replacement_amounts := [0, 0, 0, 0, 0]
var map_regional_replacement_lists := [[], [], [], [], []]
var current_texture: Texture
var minimap_mode := 0
var minimap_tween := 0.0
const map_tilesets = [0, 1, 0, 2, 2]
const waterfall_scrolls = [true, false, true, false, false]
const sea_scrolls = [true, true, true, false, false]
2024-07-05 02:11:24 +09:30
func _create_worldmap_texture(id: int) -> void:
var tileset = map_tilesets[id]
var image = MapLoader.worldmaps[id].make_tile_map()
self.map_images[id] = image
var tex := SpriteLoader.texture_from_image(image, Texture.FLAG_REPEAT)
self.map_textures[id] = tex
func _create_worldmap_textures() -> void:
2024-07-05 02:11:24 +09:30
for id in 5:
self._create_worldmap_texture(id)
for flag_block in MapLoader.worldmaps[id].event_replacements.values():
for region in flag_block:
self.map_regional_replacement_lists[id].append(region)
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)
minimap.material.set_shader_param('enable_tile_globbing', true)
2024-07-05 02:11:24 +09:30
func _jump_to_regional_replacement(region) -> void:
var y_min = region.start_y
var y_max = y_min + len(region.rows)
var x_min = region.get_min_x()
var x_max = region.get_max_x()
self.pos = Vector2((x_max+x_min)/2, (y_max+y_min)/2)
print_debug('Jumping to event position ', self.pos.x, self.pos.y)
var delay_map_update: float = -1.0
func _perform_regional_replacement(amount: int) -> void:
var map = MapLoader.worldmaps[current_map_id]
var last_region
match amount:
0:
map.blockmap = map.blockmap_original
self.map_regional_replacement_amounts[current_map_id] = 0
self.delay_map_update = 0.0001
1:
if self.map_regional_replacement_amounts[current_map_id] >= len(self.map_regional_replacement_lists[current_map_id]):
return
last_region = self.map_regional_replacement_lists[current_map_id][self.map_regional_replacement_amounts[current_map_id]]
map.apply_event_region_replacement(last_region)
self._jump_to_regional_replacement(last_region)
self.map_regional_replacement_amounts[current_map_id] += 1
self.delay_map_update = 1.0
-1:
if self.map_regional_replacement_amounts[current_map_id] < 1:
return
self.map_regional_replacement_amounts[current_map_id] -= 1
# Reset and reapply the stack
map.blockmap = map.blockmap_original
for i in self.map_regional_replacement_amounts[current_map_id]:
last_region = self.map_regional_replacement_lists[current_map_id][i]
map.apply_event_region_replacement(last_region)
# self._jump_to_regional_replacement(last_region)
self.delay_map_update = 0.0001
# self._create_worldmap_texture(current_map_id)
# self._set_map(current_map_id) # refresh shader etc.
# 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_textures()
2023-07-28 00:06:46 +09:30
# _create_palette_and_atlas_texrects()
self.material = worldmap_shader_mat.duplicate()
minimap.material = worldmap_shader_mat.duplicate()
minimap.modulate.a8 = 192
minimap.rect_scale = Vector2.ONE * 0.125
minimap.rect_position = Vector2(8, 168)
_set_map(0)
func handle_movement_input(delta: float, quantize:=8.0) -> void:
delta = round(delta*quantize)/quantize
if Input.is_physical_key_pressed(KEY_RIGHT):
self.pos.x += delta
if Input.is_physical_key_pressed(KEY_LEFT):
self.pos.x -= delta
if Input.is_physical_key_pressed(KEY_DOWN):
self.pos.y += delta
if Input.is_physical_key_pressed(KEY_UP):
self.pos.y -= delta
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
self.handle_movement_input(delta*16)
self.pos = self.pos.posmod(256.0)
self.minimap_tween -= sign(minimap_tween - minimap_mode) * delta * 4
self.minimap_tween = clamp(minimap_tween, 0, 1)
var l = ease(minimap_tween, -3.75)
# l = minimap_tween
var minimap_scale = lerp(0.125, 0.5, l)
minimap.rect_scale = Vector2.ONE * minimap_scale
minimap.rect_position = Vector2(lerp(8, 64, l), lerp(168, -8, l))
minimap.modulate.a8 = lerp(192, 240, l)
# minimap.material.set_shader_param('subtile_offset', Vector2.ONE*0/8.0)
# minimap.material.set_shader_param('uv_scale', 64 * minimap_scale)
minimap.material.set_shader_param('uv_scale', lerp(512, 1<<5, l))
# minimap.material.set_shader_param('uv_scale', 512/16)
2024-07-05 02:11:24 +09:30
# Hack to do a presentation of map region replacements with a nice before and after
if self.delay_map_update > 0.000:
self.delay_map_update -= delta
if self.delay_map_update <= 0.000:
self._create_worldmap_texture(current_map_id)
self._set_map(current_map_id) # refresh shader etc.
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_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
2024-07-05 02:11:24 +09:30
KEY_7:
self._perform_regional_replacement(0)
KEY_8:
self._perform_regional_replacement(-1)
KEY_9:
self._perform_regional_replacement(1)
KEY_H:
$lbl_help.visible = !$lbl_help.visible