Proper mob spawning.

This commit is contained in:
Relintai 2020-07-15 19:35:48 +02:00
parent 8d34206fe3
commit ae86236870
2 changed files with 39 additions and 4 deletions

View File

@ -217,7 +217,7 @@ func spawn_player(class_id : int, position : Vector3, name : String, node_name
return createinfo.created_entity
func spawn_mob(class_id : int, level : int, position : Vector3) -> void:
func spawn_mob(class_id : int, level : int, position : Vector3) -> Entity:
var createinfo : EntityCreateInfo = EntityCreateInfo.new()
var cls : EntityData = ESS.get_resource_db().get_entity_data(class_id)
@ -230,7 +230,9 @@ func spawn_mob(class_id : int, level : int, position : Vector3) -> void:
createinfo.entity_player_type = EntityEnums.ENTITY_PLAYER_TYPE_AI
createinfo.transform2d.origin = Vector2(position.x, position.y)
ESS.request_entity_spawn_deferred(createinfo)
ESS.request_entity_spawn(createinfo)
return createinfo.created_entity
# print("Mob spawned " + str(createinfo))

View File

@ -41,9 +41,11 @@ 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
export(int) var enemy_count : int = 14
var map : Array = []
var rooms : Array = []
var enemies : Array = []
onready var tile_map : TileMap = $Terrarin
onready var visibility_map : TileMap = $VisibilityMap
@ -58,8 +60,8 @@ func load_character(file_name: String) -> void:
randomize()
build_level()
if spawn_mobs:
generate()
# if spawn_mobs:
# generate()
#Place player
var start_room = rooms.front()
@ -69,6 +71,27 @@ func load_character(file_name: String) -> void:
_player = ESS.entity_spawner.load_player(_player_file_name, pos, 1) as Entity
Server.sset_seed(_player.sseed)
#Place enemies
for i in range(enemy_count):
var room = rooms[1 + randi() % (rooms.size() - 1)]
var x = room.position.x + 1 + randi() % int (room.size.x - 2)
var y = room.position.y + 1 + randi() % int (room.size.y - 2)
var blocked = false
for enemy in enemies:
var body = enemy.get_body()
var bp = body.get_tile_position()
if bp.x == x && bp.y == y:
blocked = true
break
if !blocked:
var t = tile_to_pixel_center(x, y)
var enemy = ESS.entity_spawner.spawn_mob(1, 1, Vector3(t.x, t.y, 0))
enemies.append(enemy)
tile_map.update_dirty_quadrants()
call_deferred("update_visibility")
@ -112,6 +135,11 @@ func is_position_walkable(x : int, y : int) -> bool:
elif type == Tile.Stone:
return false
for e in enemies:
var pos : Vector2 = e.get_body().get_tile_position()
if pos.x == x && pos.y == y:
return false
return true
func build_level():
@ -119,6 +147,11 @@ func build_level():
map.clear()
tile_map.clear()
for e in enemies:
e.queue_free()
enemies.clear()
for x in range(level_size.x):
map.append([])
for y in range(level_size.y):