Level generation and player movement based on https://www.youtube.com/watch?v=vQ1UGbUlzH4 .

This commit is contained in:
Relintai 2020-07-15 16:26:14 +02:00
parent 1e14874db8
commit f6ce594362
7 changed files with 410 additions and 121 deletions

View File

@ -1,17 +1,9 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=3 format=2]
[ext_resource path="res://characters/SimpleCharacter.tscn" type="PackedScene" id=1]
[ext_resource path="res://player/Body.gd" type="Script" id=6]
[sub_resource type="CircleShape2D" id=1]
radius = 8.0
[node name="Body" type="KinematicBody2D"]
[node name="Body" type="Node2D"]
script = ExtResource( 6 )
[node name="Character" parent="." instance=ExtResource( 1 )]
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 1 )
[node name="Ray" type="RayCast2D" parent="."]

View File

@ -21,7 +21,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
extends KinematicBody2D
extends Node2D
export(String) var world_path : String = "../.."
export(NodePath) var character_skeleton_path : NodePath = "Character"
@ -54,12 +54,16 @@ var character_skeleton : CharacterSkeleton2D
var visibility_update_timer : float = randi()
var ray : RayCast2D
var tile_size : int = 32
func _enter_tree() -> void:
ray = $Ray
world = get_node(world_path) as Node
tile_size = get_node("/root/Main").get_tile_size()
camera = get_node_or_null("Camera") as Camera2D
set_process_input(false)
set_process_unhandled_input(false)
character_skeleton = get_node(character_skeleton_path)
entity = get_node("..")
@ -73,10 +77,8 @@ func _enter_tree() -> void:
transform = entity.get_transform_2d(true)
set_physics_process(true)
set_process_input(false)
set_process_unhandled_input(false)
# set_physics_process(true)
func _process(delta : float) -> void:
if entity.ai_state == EntityEnums.AI_STATE_OFF:
@ -97,36 +99,36 @@ func _process(delta : float) -> void:
if l < rs:
if not visible:
show()
set_physics_process(true)
# set_physics_process(true)
else:
if visible:
hide()
set_physics_process(false)
# set_physics_process(false)
func _physics_process(delta : float) -> void:
if entity.sentity_data == null:
return
if dead:
return
if entity.c_is_controlled:
process_movement(delta)
else:
if sleep:
sleep_recheck_timer += delta
if sleep_recheck_timer < 0.5:
return
sleep_recheck_timer = 0
# if world != null:
# if not world.is_position_walkable(transform.origin):
#func _physics_process(delta : float) -> void:
# if entity.sentity_data == null:
# return
#
# if dead:
# return
#
# if entity.c_is_controlled:
# process_movement(delta)
# else:
# if sleep:
# sleep_recheck_timer += delta
#
# if sleep_recheck_timer < 0.5:
# return
process_movement(delta)
#
# sleep_recheck_timer = 0
#
## if world != null:
## if not world.is_position_walkable(transform.origin):
## return
#
# process_movement(delta)
func process_movement(delta : float) -> void:
@ -167,28 +169,24 @@ func process_movement(delta : float) -> void:
# rpc_id(1, "sset_position", position)
# else:
# sset_position(position)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventKey:
var ievkey : InputEventKey = event as InputEventKey
#Not sure why yet, but _unhandled_input gets called even after set_process_unhandled_input(false)
if !entity.c_is_controlled:
return
if ievkey.echo:
return
var val : int = 1
if not event.pressed:
val = -1
if ievkey.scancode == KEY_W:
input_direction.y -= val
if ievkey.scancode == KEY_S:
input_direction.y += val
if ievkey.scancode == KEY_A:
input_direction.x -= val
if ievkey.scancode == KEY_D:
input_direction.x += val
if event.is_action_pressed("left"):
try_move(-1, 0)
return
elif event.is_action_pressed("right"):
try_move(1, 0)
return
elif event.is_action_pressed("up"):
try_move(0, -1)
return
elif event.is_action_pressed("down"):
try_move(0, 1)
return
if event is InputEventMouseMotion and event.device != -1:
cmouseover(event)
@ -227,48 +225,70 @@ func _unhandled_input(event: InputEvent) -> void:
func target(position : Vector2) -> bool:
var space_state = get_world_2d().direct_space_state
var results = space_state.intersect_point(world.make_canvas_position_local(position), 32, [], get_collision_layer())
#var results = space_state.intersect_point(position, 32, [], 2)
func try_move(dx, dy):
var tp : Vector2 = get_tile_position()
tp.x += dx
tp.y += dy
if !world.is_position_walkable(tp.x, tp.y):
return
#todo
#world.can_interact
set_tile_position(tp)
func get_tile_position() -> Vector2:
var v : Vector2 = Vector2(int(transform.origin.x / tile_size), int(transform.origin.y / tile_size))
return v
func set_tile_position(pos : Vector2) -> void:
transform.origin = pos * tile_size + Vector2(tile_size / 2, tile_size / 2)
if results:
for result in results:
if result.collider and result.collider.owner is Entity:
entity.target_crequest_change((result.collider.owner as Node).get_path())
return true
entity.target_crequest_change(NodePath())
else:
entity.target_crequest_change(NodePath())
func target(position : Vector2) -> bool:
# var space_state = get_world_2d().direct_space_state
# var results = space_state.intersect_point(world.make_canvas_position_local(position), 32, [], get_collision_layer())
# #var results = space_state.intersect_point(position, 32, [], 2)
#
# if results:
# for result in results:
# if result.collider and result.collider.owner is Entity:
# entity.target_crequest_change((result.collider.owner as Node).get_path())
# return true
#
# entity.target_crequest_change(NodePath())
# else:
# entity.target_crequest_change(NodePath())
#
return false
func cmouseover(event):
var space_state = get_world_2d().direct_space_state
var results = space_state.intersect_point(world.make_canvas_position_local(position), 32, [], get_collision_layer())
#var results = space_state.intersect_point(position, 32, [], 2)
if results:
for result in results:
if result.collider and result.collider.owner is Entity:
var mo : Entity = result.collider.owner as Entity
if last_mouse_over != null and last_mouse_over != mo:
if is_instance_valid(last_mouse_over):
last_mouse_over.notification_cmouse_exit()
last_mouse_over = null
if last_mouse_over == null:
mo.notification_cmouse_enter()
last_mouse_over = mo
return
if last_mouse_over != null:
last_mouse_over.notification_cmouse_exit()
last_mouse_over = null
# var space_state = get_world_2d().direct_space_state
# var results = space_state.intersect_point(world.make_canvas_position_local(position), 32, [], get_collision_layer())
# #var results = space_state.intersect_point(position, 32, [], 2)
#
# if results:
# for result in results:
# if result.collider and result.collider.owner is Entity:
# var mo : Entity = result.collider.owner as Entity
#
# if last_mouse_over != null and last_mouse_over != mo:
# if is_instance_valid(last_mouse_over):
# last_mouse_over.notification_cmouse_exit()
#
# last_mouse_over = null
#
# if last_mouse_over == null:
# mo.notification_cmouse_enter()
# last_mouse_over = mo
#
# return
#
# if last_mouse_over != null:
# last_mouse_over.notification_cmouse_exit()
# last_mouse_over = null
pass
func on_c_controlled_changed(val):
@ -286,14 +306,14 @@ func on_c_controlled_changed(val):
var ui = uiscn.instance()
add_child(ui)
set_process_input(true)
# set_process_input(true)
set_process_unhandled_input(true)
else:
if camera:
camera.queue_free()
camera = null
set_process_input(false)
# set_process_input(false)
set_process_unhandled_input(false)
var nameplatescn : PackedScene = ResourceLoader.load("res://ui/nameplates/NamePlate.tscn")
var nameplate = nameplatescn.instance()

View File

@ -48,7 +48,20 @@ var health : EntityResource = null
var _health : EntityResource
var offset : int = 60
func _enter_tree():
var m = get_node_or_null("/root/Main")
if m != null:
var ts = m.get_tile_size()
if ts == 8:
offset = 20
elif ts == 16:
offset = 24
elif ts == 32:
offset = 34
name_label = get_node(name_label_path) as Label
health_bar = get_node(health_bar_path) as TextureProgress
health_bar_label = get_node(health_bar_label_path) as Label
@ -84,7 +97,7 @@ func _process(delta):
var position : Vector2 = entity.get_body().position
position.x -= (rect_size.x / 2.0) * rect_scale.x
position.y -= 60
position.y -= offset
set_position(position)

View File

@ -105,7 +105,7 @@ func load_player(file_name : String, position : Vector3, network_owner : int) ->
createinfo.entity_controller = EntityEnums.ENITIY_CONTROLLER_PLAYER
createinfo.entity_player_type = EntityEnums.ENTITY_PLAYER_TYPE_PLAYER
createinfo.serialized_data = load_file(file_name)
createinfo.transform.origin = position
createinfo.transform2d.origin = Vector2(position.x, position.y)
createinfo.networked = false
# print("Player spawned ")
ESS.request_entity_spawn(createinfo)

View File

@ -497,6 +497,26 @@ sheath={
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":80,"unicode":0,"echo":false,"script":null)
]
}
up={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null)
]
}
down={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null)
]
}
left={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
]
}
right={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null)
]
}
[layer_names]

View File

@ -34,32 +34,276 @@ var _editor_generate : bool
var _player_file_name : String
var _player : Entity
enum Tile { Floor, Wall, Door, Ladder, Stone }
var tile_size : int = 32
export(Vector2) var level_size : Vector2 = Vector2(40, 40)
export(int) var level_room_count : int = 9
export(int) var min_room_dimension : int = 5
export(int) var max_room_dimension : int = 8
var map : Array = []
var rooms : Array = []
onready var tile_map : TileMap = $Terrarin
func _ready():
# print(get_layer(2))
tile_size = get_node("/root/Main").get_tile_size()
pass # Replace with function body.
func get_layer(index : int) -> Navigation2D:
for ch in get_children():
if ch.has_method('collision_layer') and ch.collision_layer() == index:
return ch
var wl : Navigation2D = world_layer.instance() as Navigation2D
add_child(wl)
wl.collision_layer = index
return wl
func load_character(file_name: String) -> void:
_player_file_name = file_name
_player = ESS.entity_spawner.load_player(file_name, Vector3(5, 5, 0), 1) as Entity
#TODO hack, do this properly
# _player.set_physics_process(false)
Server.sset_seed(_player.sseed)
randomize()
build_level()
if spawn_mobs:
generate()
#Place player
var start_room = rooms.front()
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 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
Server.sset_seed(_player.sseed)
func is_position_walkable(x : int, y : int) -> bool:
var type = map[x][y]
if type == Tile.Wall:
return false
elif type == Tile.Stone:
return false
return true
func build_level():
rooms.clear()
map.clear()
tile_map.clear()
for x in range(level_size.x):
map.append([])
for y in range(level_size.y):
map[x].append(Tile.Stone)
tile_map.set_cell(x, y, Tile.Stone)
var free_regions = [Rect2(Vector2(2, 2), level_size - Vector2(4, 4))]
for i in range(level_room_count):
add_room(free_regions)
if free_regions.empty():
break
connect_rooms()
func connect_rooms():
var stone_graph : AStar2D = AStar2D.new()
var point_id : int = 0
for x in range(level_size.x):
for y in range(level_size.y):
if map[x][y] == Tile.Stone:
stone_graph.add_point(point_id, Vector2(x, y))
#connect to left if also stone
if x > 0 && map[x - 1][y] == Tile.Stone:
var left_point = stone_graph.get_closest_point(Vector2(x - 1, y))
stone_graph.connect_points(point_id, left_point)
#connect to above if also stone
if y > 0 && map[x][y - 1] == Tile.Stone:
var above_point = stone_graph.get_closest_point(Vector2(x, y - 1))
stone_graph.connect_points(point_id, above_point)
point_id += 1
#Build an AStar graph of room connections
var room_graph : AStar2D = AStar2D.new()
point_id = 0
for room in rooms:
var room_center = room.position + room.size / 2
room_graph.add_point(point_id, Vector2(room_center.x, room_center.y))
point_id += 1
#Add random connections until everything is connected
while !is_everything_connected(room_graph):
add_random_connection(stone_graph, room_graph)
func is_everything_connected(graph : AStar2D):
var points = graph.get_points()
var start = points.pop_back()
for point in points:
var path = graph.get_point_path(start, point)
if !path:
return false
return true
func add_random_connection(stone_graph : AStar2D, room_graph : AStar2D):
#Pick rooms to connect
var start_room_id = get_least_connected_point(room_graph)
var end_room_id = get_nearest_unconnected_point(room_graph, start_room_id)
#Pick door locations
var start_position = pick_random_door_location(rooms[start_room_id])
var end_position = pick_random_door_location(rooms[end_room_id])
#Find a path to connect the doors to each other
var closest_start_point = stone_graph.get_closest_point(start_position)
var closest_end_point = stone_graph.get_closest_point(end_position)
var path = stone_graph.get_point_path(closest_start_point, closest_end_point)
assert(path)
#Add path to the map
set_tile(start_position.x, start_position.y, Tile.Door)
set_tile(end_position.x, end_position.y, Tile.Door)
for position in path:
set_tile(position.x, position.y, Tile.Floor)
room_graph.connect_points(start_room_id, end_room_id)
func get_least_connected_point(graph : AStar2D):
var point_ids = graph.get_points()
var least
var tied_for_least = []
for point in point_ids:
var count = graph.get_point_connections(point).size()
if !least || count < least:
least = count
tied_for_least = [point]
elif count == least:
tied_for_least.append(point)
return tied_for_least[randi() % tied_for_least.size()]
func get_nearest_unconnected_point(graph : AStar2D, target_point):
var target_position = graph.get_point_position(target_point)
var point_ids = graph.get_points()
var nearest
var tied_for_nearest = []
for point in point_ids:
if point == target_point:
continue
var path = graph.get_point_path(point, target_point)
if path:
continue
var dist = (graph.get_point_position(point) - target_position).length()
if !nearest || dist < nearest:
nearest = dist
tied_for_nearest = [point]
elif dist == nearest:
tied_for_nearest.append(point)
return tied_for_nearest[randi() % tied_for_nearest.size()]
func pick_random_door_location(room):
var options = []
#Top and bottom walls
for x in range(room.position.x + 1, room.end.x - 2):
options.append(Vector2(x, room.position.y))
options.append(Vector2(x, room.end.y))
#Left and right walls
for y in range(room.position.y + 1, room.end.y - 2):
options.append(Vector2(room.position.x, y))
options.append(Vector2(room.end.x, y))
return options[randi() % options.size()]
func add_room(free_regions):
var region = free_regions[randi() % free_regions.size()]
var size_x = min_room_dimension
if region.size.x > min_room_dimension:
size_x += randi() % int(region.size.x - min_room_dimension)
var size_y = min_room_dimension
if region.size.y > min_room_dimension:
size_y += randi() % int(region.size.y - min_room_dimension)
size_x = min(size_x, min_room_dimension)
size_y = min(size_y, min_room_dimension)
var start_x = region.position.x
if region.size.x > size_x:
start_x += randi() % int(region.size.x - size_x)
var start_y = region.position.y
if region.size.y > size_y:
start_y += randi() % int(region.size.y - size_y)
var room = Rect2(start_x, start_y, size_x, size_y)
rooms.append(room)
for x in range(start_x, start_x + size_x):
set_tile(x, start_y, Tile.Wall)
set_tile(x, start_y + size_y - 1, Tile.Wall)
for y in range(start_y, start_y + size_y):
set_tile(start_x, y, Tile.Wall)
set_tile(start_x + size_x - 1, y, Tile.Wall)
for x in range(start_x + 1, start_x + size_x - 1):
set_tile(x, y, Tile.Floor)
cut_regions(free_regions, room)
func cut_regions(free_regions, region_to_remove):
var removal_queue = []
var addition_queue = []
for region in free_regions:
if region.intersects(region_to_remove):
removal_queue.append(region)
var leftover_left = region_to_remove.position.x - region.position.x - 1
var leftover_right = region_to_remove.end.x - region_to_remove.end.x - 1
var leftover_above = region_to_remove.position.y - region.position.y - 1
var leftover_below = region.end.y - region_to_remove.end.y - 1
if leftover_left >= min_room_dimension:
addition_queue.append(Rect2(region.position, Vector2(leftover_left, region.size.y)))
if leftover_right >= min_room_dimension:
addition_queue.append(Rect2(Vector2(region_to_remove.end.x + 1, region.position.y), Vector2(leftover_right, region.size.y)))
if leftover_above >= min_room_dimension:
addition_queue.append(Rect2(region.position, Vector2(region.size.x, leftover_above)))
if leftover_below >= min_room_dimension:
addition_queue.append(Rect2(Vector2(region.position.x, region_to_remove.end.y + 1), Vector2(region.size.x, leftover_below)))
for region in removal_queue:
free_regions.erase(region)
for region in addition_queue:
free_regions.append(region)
func set_tile(x, y, type):
map[x][y] = type
tile_map.set_cell(x, y, type)
func generate() -> void:
for x in range(-5, 5):
for y in range(-5, 5):

File diff suppressed because one or more lines are too long