Implement support for multiple levels.

This commit is contained in:
Relintai 2020-07-16 19:36:14 +02:00
parent df0584a9e1
commit 7843e75599
10 changed files with 158 additions and 121 deletions

View File

@ -58,10 +58,15 @@ var tile_size : int = 32
var nameplate : Node var nameplate : Node
var init : bool = false
func _enter_tree() -> void: func _enter_tree() -> void:
world = get_node(world_path) as Node2D world = get_node(world_path) as Node2D
tile_size = get_node("/root/Main").get_tile_size() tile_size = get_node("/root/Main").get_tile_size()
if init:
return
camera = get_node_or_null("Camera") as Camera2D camera = get_node_or_null("Camera") as Camera2D
set_process_input(false) set_process_input(false)
@ -78,6 +83,8 @@ func _enter_tree() -> void:
on_c_controlled_changed(entity.c_is_controlled) on_c_controlled_changed(entity.c_is_controlled)
transform = entity.get_transform_2d(true) transform = entity.get_transform_2d(true)
init = true
func set_visibility(val : bool) -> void: func set_visibility(val : bool) -> void:
if val: if val:
@ -282,9 +289,7 @@ func target(position : Vector2) -> bool:
return false return false
func cmouseover(position : Vector2): func cmouseover(position : Vector2):
#https://github.com/godotengine/godot/issues/32222 position = world.make_canvas_position_local(position)
position = position - get_viewport_transform().origin
position *= camera.zoom
var pos : Vector2 = world.pixel_to_tile(position.x, position.y) var pos : Vector2 = world.pixel_to_tile(position.x, position.y)
var enemy : Entity = world.get_enemy_at_tile(pos.x, pos.y) var enemy : Entity = world.get_enemy_at_tile(pos.x, pos.y)

View File

@ -217,7 +217,7 @@ func spawn_player(class_id : int, position : Vector3, name : String, node_name
return createinfo.created_entity return createinfo.created_entity
func spawn_mob(class_id : int, level : int, position : Vector3) -> Entity: func spawn_mob(class_id : int, level : int, position : Vector3, parent_path : String = "") -> Entity:
var createinfo : EntityCreateInfo = EntityCreateInfo.new() var createinfo : EntityCreateInfo = EntityCreateInfo.new()
var cls : EntityData = ESS.get_resource_db().get_entity_data(class_id) var cls : EntityData = ESS.get_resource_db().get_entity_data(class_id)
@ -229,6 +229,7 @@ func spawn_mob(class_id : int, level : int, position : Vector3) -> Entity:
createinfo.entity_controller = EntityEnums.ENITIY_CONTROLLER_AI createinfo.entity_controller = EntityEnums.ENITIY_CONTROLLER_AI
createinfo.entity_player_type = EntityEnums.ENTITY_PLAYER_TYPE_AI createinfo.entity_player_type = EntityEnums.ENTITY_PLAYER_TYPE_AI
createinfo.transform2d.origin = Vector2(position.x, position.y) createinfo.transform2d.origin = Vector2(position.x, position.y)
createinfo.parent_path = parent_path
ESS.request_entity_spawn(createinfo) ESS.request_entity_spawn(createinfo)

View File

@ -27,11 +27,10 @@ export(bool) var editor_generate : bool = false setget set_editor_generate, get_
export(bool) var show_loading_screen : bool = true export(bool) var show_loading_screen : bool = true
export(bool) var generate_on_ready : bool = false export(bool) var generate_on_ready : bool = false
var initial_generation : bool = false var generated : bool = false
var _editor_generate : bool var _editor_generate : bool
var _player_file_name : String
var _player : Entity var _player : Entity
enum Tile { Floor, Wall, Door, Ladder, Stone } enum Tile { Floor, Wall, Door, Ladder, Stone }
@ -47,17 +46,30 @@ var map : Array = []
var rooms : Array = [] var rooms : Array = []
var enemies : Array = [] var enemies : Array = []
var nav_graph : AStar2D var nav_graph : AStar2D
var entrance_position : Transform2D = Transform2D()
onready var tile_map : TileMap = $Terrarin onready var tile_map : TileMap = $Terrarin
onready var visibility_map : TileMap = $VisibilityMap onready var visibility_map : TileMap = $VisibilityMap
func _ready(): func _ready():
tile_size = get_node("/root/Main").get_tile_size() tile_size = get_node("/root/Main").get_tile_size()
connect("visibility_changed", self, "on_visibility_changed")
pass # Replace with function body. pass # Replace with function body.
func load_character(file_name: String) -> void: func place_player(player: Entity) -> void:
_player_file_name = file_name _player = player
if (_player == null):
return
if !generated:
build()
_player.get_body().transform = entrance_position
call_deferred("update_visibility")
func build():
randomize() randomize()
build_level() build_level()
@ -65,8 +77,9 @@ func load_character(file_name: String) -> void:
var start_room = rooms.front() var start_room = rooms.front()
var player_x = start_room.position.x + 1 + randi() % int(start_room.size.x - 2) var player_x = start_room.position.x + 1 + randi() % int(start_room.size.x - 2)
var player_y = start_room.position.y + 1 + randi() % int(start_room.size.y - 2) var player_y = start_room.position.y + 1 + randi() % int(start_room.size.y - 2)
var pos : Vector3 = Vector3(player_x * tile_size + tile_size / 2, player_y * tile_size + tile_size / 2, 0)
_player = ESS.entity_spawner.load_player(_player_file_name, pos, 1) as Entity entrance_position.origin = Vector2(player_x * tile_size + tile_size / 2, player_y * tile_size + tile_size / 2)
# _player = ESS.entity_spawner.load_player(_player_file_name, pos, 1) as Entity
#Server.sset_seed(_player.sseed) #Server.sset_seed(_player.sseed)
#Place enemies #Place enemies
@ -85,14 +98,13 @@ func load_character(file_name: String) -> void:
if !blocked: if !blocked:
var t = tile_to_pixel_center(x, y) var t = tile_to_pixel_center(x, y)
var enemy = ESS.entity_spawner.spawn_mob(1, 1, Vector3(t.x, t.y, 0)) var enemy = ESS.entity_spawner.spawn_mob(1, 1, Vector3(t.x, t.y, 0), get_path())
enemies.append(enemy) enemies.append(enemy)
tile_map.update_dirty_quadrants() tile_map.update_dirty_quadrants()
call_deferred("update_visibility") generated = true
func player_moved(): func player_moved():
_player.update(1) _player.update(1)
@ -103,6 +115,12 @@ func player_moved():
call_deferred("update_visibility") call_deferred("update_visibility")
func update_visibility(): func update_visibility():
if !visible:
return
if tile_map.collision_layer == 2:
return
if _player == null: if _player == null:
return return
@ -112,7 +130,7 @@ func update_visibility():
return return
var tp : Vector2 = body.get_tile_position() var tp : Vector2 = body.get_tile_position()
var space_state : Physics2DDirectSpaceState = get_world_2d().direct_space_state var space_state : Physics2DDirectSpaceState = get_world_2d().direct_space_state
for x in range(level_size.x): for x in range(level_size.x):
@ -122,7 +140,7 @@ func update_visibility():
var y_dir = 1 if y < tp.y else -1 var y_dir = 1 if y < tp.y else -1
var test_point = tile_to_pixel_center(x, y) + Vector2(x_dir, y_dir) * tile_size / 2 var test_point = tile_to_pixel_center(x, y) + Vector2(x_dir, y_dir) * tile_size / 2
var occlusion = space_state.intersect_ray(body.transform.origin, test_point) var occlusion = space_state.intersect_ray(body.transform.origin, test_point, [], 1)
if !occlusion || (occlusion.position - test_point).length() < 1: if !occlusion || (occlusion.position - test_point).length() < 1:
visibility_map.set_cell(x, y, -1) visibility_map.set_cell(x, y, -1)
@ -133,11 +151,15 @@ func update_visibility():
if !b.visible: if !b.visible:
var pos : Vector2 = b.transform.origin var pos : Vector2 = b.transform.origin
var occlusion = space_state.intersect_ray(body.transform.origin, pos) var occlusion = space_state.intersect_ray(body.transform.origin, pos, [], 1)
if !occlusion: if !occlusion:
b.set_visibility(true) b.set_visibility(true)
e.sets_target(_player) e.sets_target(_player)
# tile_map.update_dirty_quadrants()
# visibility_map.update_dirty_quadrants()
func clear_path(tile): func clear_path(tile):
var new_point = nav_graph.get_available_point_id() var new_point = nav_graph.get_available_point_id()
@ -425,12 +447,6 @@ func set_tile(x, y, type):
if type == Tile.Floor: if type == Tile.Floor:
clear_path(Vector2(x, y)) clear_path(Vector2(x, y))
func save() -> void:
if _player == null or _player_file_name == "":
return
ESS.entity_spawner.save_player(_player, _player_file_name)
func _generation_finished(): func _generation_finished():
@ -456,3 +472,20 @@ func set_editor_generate(value : bool) -> void:
pass pass
_editor_generate = value _editor_generate = value
func on_visibility_changed():
if visible:
if tile_map.collision_layer != 1:
tile_map.collision_layer = 1
else:
if tile_map.collision_layer != 2:
tile_map.collision_layer = 2
for e in enemies:
var b = e.get_body()
if b.visible:
b.set_visibility(false)
e.sets_target(null)

View File

@ -132,8 +132,8 @@ func switch_scene(scene : int) -> void:
current_scene = gs current_scene = gs
elif scene == StartSceneTypes.WORLD: elif scene == StartSceneTypes.WORLD:
# var gs : Node = world_scene.instance() var gs : Node = world_scene.instance()
var gs : Node = get_world().instance() # var gs : Node = get_world().instance()
add_child(gs) add_child(gs)
gs.owner = self gs.owner = self
@ -163,7 +163,7 @@ class ModulePathSorter:
return false return false
func get_world(): func get_world():
return worlds[curent_style] return world_scene
func get_world_scale(): func get_world_scale():
return world_scales[curent_style] return world_scales[curent_style]

76
game/scenes/World.gd Normal file
View File

@ -0,0 +1,76 @@
extends Node2D
var _player_file_name : String
var _player : Entity
export(Array, NodePath) var level_paths : Array
var levels : Array
var current_level : int = -1
func _ready():
for lp in level_paths:
var l = get_node(lp)
if l.visible:
l.hide()
levels.append(l)
func _unhandled_key_input(event):
if event.scancode == KEY_M and event.pressed:
var l : int = current_level + 1
if l >= levels.size():
l = 0
switch_to_level(l)
func switch_to_level(level_index : int):
_player.get_body().hide()
if current_level != -1:
levels[current_level].hide()
current_level = level_index
levels[current_level].show()
if _player.get_parent():
_player.get_parent().place_player(null)
_player.get_parent().remove_child(_player)
levels[current_level].add_child(_player)
levels[current_level].place_player(_player)
# _player.get_body().world = levels[current_level]
_player.get_body().show()
func load_character(file_name: String) -> void:
_player_file_name = file_name
_player = ESS.entity_spawner.load_player(_player_file_name, Vector3.ZERO, 1) as Entity
_player.get_body().hide()
#Server.sset_seed(_player.sseed)
call_deferred("switch_to_level", 0)
func save() -> void:
if _player == null or _player_file_name == "":
return
ESS.entity_spawner.save_player(_player, _player_file_name)
#so body won't have to check things every time
func pixel_to_tile(x, y):
return Vector2.ZERO
func tile_to_pixel_center(x, y):
return Vector2.ZERO
func is_position_walkable(x : int, y : int) -> bool:
return false
func get_enemy_at_tile(x : int, y : int) -> Entity:
return null
func place_player(player: Entity) -> void:
return

File diff suppressed because one or more lines are too long

View File

@ -1,63 +0,0 @@
[gd_scene load_steps=4 format=2]
[ext_resource path="res://scripts/settings/DirectionalLightSettings.gd" type="Script" id=3]
[sub_resource type="ProceduralSky" id=1]
sky_top_color = Color( 0.435294, 0.423529, 0.313726, 1 )
sky_horizon_color = Color( 0.309804, 0.317647, 0.219608, 1 )
sky_curve = 0.263535
sky_energy = 0.3
ground_bottom_color = Color( 0.196078, 0.152941, 0.152941, 1 )
ground_horizon_color = Color( 0.223529, 0.192157, 0.164706, 1 )
ground_curve = 0.101965
ground_energy = 0.4
sun_color = Color( 0.356863, 0.313726, 0.219608, 1 )
sun_latitude = 39.71
sun_longitude = -8.09
sun_angle_min = 0.0
sun_angle_max = 23.15
sun_energy = 9.29
texture_size = 0
[sub_resource type="Environment" id=2]
background_mode = 2
background_sky = SubResource( 1 )
ambient_light_color = Color( 0.870588, 0.870588, 0.870588, 1 )
ambient_light_energy = 1.67
ambient_light_sky_contribution = 0.09
fog_enabled = true
fog_color = Color( 0.184314, 0.207843, 0.156863, 1 )
fog_sun_color = Color( 0.196078, 0.266667, 0.309804, 1 )
fog_sun_amount = 0.53
fog_depth_begin = 155.9
fog_depth_end = 379.9
fog_depth_curve = 1.18921
tonemap_mode = 2
tonemap_exposure = 0.83
auto_exposure_max_luma = 7.33
ss_reflections_enabled = true
ssao_enabled = true
glow_levels/3 = false
glow_intensity = 1.6
glow_strength = 1.1
glow_bloom = 0.1
glow_hdr_luminance_cap = 1.0
[node name="World" type="Spatial" groups=[
"save",
]]
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource( 2 )
__meta__ = {
"_editor_description_": ""
}
[node name="DirectionalLight" type="DirectionalLight" parent="."]
transform = Transform( -0.797163, 0.45442, 0.397535, 0.124932, -0.520028, 0.844963, 0.590697, 0.723238, 0.357776, 0, 18.834, 0 )
layers = 3
light_color = Color( 1, 0.878431, 0.878431, 1 )
light_energy = 0.87
light_specular = 0.65
shadow_bias = 0.07
script = ExtResource( 3 )

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long