You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Gridmap/scripts/World.gd

97 lines
3.3 KiB

extends Node
class_name World
var heightMap: Array
var temperatureMap: Array
var moistureMap: Array
var blocs: Array
var entities: Array
var width: int
var height: int
var chunk_size = 64
var chunk_number = 6
func get_neighbours_8_at_same_height(point: Vector2i):
var neighbours = 0
var current_bloc = get_height(Vector2i(point.x, point.y))
if get_height(Vector2i(point.x-1, point.y-1)) == current_bloc:
neighbours += Global.directions_8.TOP_LEFT
if get_height(Vector2i(point.x, point.y-1)) == current_bloc:
neighbours += Global.directions_8.TOP
if get_height(Vector2i(point.x+1,point.y-1)) == current_bloc:
neighbours += Global.directions_8.TOP_RIGHT
if get_height(Vector2i(point.x+1, point.y)) == current_bloc:
neighbours += Global.directions_8.RIGHT
if get_height(Vector2i(point.x+1, point.y+1)) == current_bloc:
neighbours += Global.directions_8.BOTTOM_RIGHT
if get_height(Vector2i(point.x, point.y+1)) == current_bloc:
neighbours += Global.directions_8.BOTTOM
if get_height(Vector2i(point.x-1, point.y+1)) == current_bloc:
neighbours += Global.directions_8.BOTTOM_LEFT
if get_height(Vector2i(point.x-1, point.y)) == current_bloc:
neighbours += Global.directions_8.LEFT
return neighbours
func get_neighbours_4_at_same_height(point: Vector2i):
var neighbours = 0
var current_bloc = get_height(Vector2i(point.x, point.y))
if get_height(Vector2i(point.x, point.y-1)) == current_bloc:
neighbours += Global.directions_4.TOP
if get_height(Vector2i(point.x+1, point.y)) == current_bloc:
neighbours += Global.directions_4.RIGHT
if get_height(Vector2i(point.x, point.y+1)) == current_bloc:
neighbours += Global.directions_4.BOTTOM
if get_height(Vector2i(point.x-1, point.y)) == current_bloc:
neighbours += Global.directions_4.LEFT
return neighbours
func neighbours(point: Vector2i):
var neighbours = []
return [
get_real_coordinates(Vector2i(point.x, point.y - 1)),
get_real_coordinates(Vector2i(point.x + 1, point.y)),
get_real_coordinates(Vector2i(point.x, point.y + 1)),
get_real_coordinates(Vector2i(point.x - 1, point.y)),
get_real_coordinates(Vector2i(point.x + 1, point.y - 1)),
get_real_coordinates(Vector2i(point.x + 1, point.y + 1)),
get_real_coordinates(Vector2i(point.x - 1, point.y + 1)),
get_real_coordinates(Vector2i(point.x - 1, point.y - 1))
]
func get_real_coordinates(point: Vector2i):
return Vector2i(fposmod(point.x, width), fposmod(point.y, height))
func get_bloc(point: Vector2i):
point = get_real_coordinates(point)
var bloc = {}
bloc["position"] = Vector3i(point.x, get_height(Vector2i(point.x, point.y)), point.y)
for key in blocs[point.x][point.y].keys():
bloc[key] = blocs[point.x][point.y][key]
return bloc
func get_height(point: Vector2i):
point = get_real_coordinates(point)
return heightMap[point.x][point.y]
func add_entity(position: Vector2i):
entities.append(Entity.new(entities.size(), position))
blocs[position.x][position.y]["entity"] = entities.size() - 1
return entities[entities.size() - 1]
func cost(point1: Vector2i, point2: Vector2i):
var cost = 1
if (
(point1.x == point2.x + 1 and point1.y == point2.y + 1) or
(point1.x == point2.x + 1 and point1.y == point2.y - 1) or
(point1.x == point2.x - 1 and point1.y == point2.y + 1) or
(point1.x == point2.x - 1 and point1.y == point2.y - 1)
):
cost = 1.1
return cost