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

138 lines
4.8 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
var player_position: Vector2i
signal new_entity
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 and get_bloc(Vector2i(point.x, point.y-1)).type != Global.blocs.WATER:
neighbours += Global.directions_4.TOP
if get_height(Vector2i(point.x+1, point.y)) == current_bloc and get_bloc(Vector2i(point.x+1, point.y)).type != Global.blocs.WATER:
neighbours += Global.directions_4.RIGHT
if get_height(Vector2i(point.x, point.y+1)) == current_bloc and get_bloc(Vector2i(point.x, point.y+1)).type != Global.blocs.WATER:
neighbours += Global.directions_4.BOTTOM
if get_height(Vector2i(point.x-1, point.y)) == current_bloc and get_bloc(Vector2i(point.x-1, point.y)).type != Global.blocs.WATER:
neighbours += Global.directions_4.LEFT
return neighbours
func neighbours(point: Vector2i):
return {
"top": get_real_coordinates(Vector2i(point.x, point.y - 1)),
"right": get_real_coordinates(Vector2i(point.x + 1, point.y)),
"bottom": get_real_coordinates(Vector2i(point.x, point.y + 1)),
"left": get_real_coordinates(Vector2i(point.x - 1, point.y)),
"top_right": get_real_coordinates(Vector2i(point.x + 1, point.y - 1)),
"bottom_right": get_real_coordinates(Vector2i(point.x + 1, point.y + 1)),
"bottom_left": get_real_coordinates(Vector2i(point.x - 1, point.y + 1)),
"top_left": get_real_coordinates(Vector2i(point.x - 1, point.y - 1))
}
func normal(point: Vector2i):
var neighbours = neighbours(point)
return Vector3(
2 * (neighbours.right - neighbours.left),
2 * (neighbours.bottom - neighbours.top),
-4
).normalized()
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)
bloc["moisture"] = get_moisture(Vector2i(point.x, 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 get_moisture(point: Vector2i):
point = get_real_coordinates(point)
return moistureMap[point.x][point.y]
func add_entity(name: String, position: Vector2i):
entities.append(load(get_custom_class(name)).new(entities.size(), position))
blocs[position.x][position.y]["entity"] = entities.size() - 1
emit_signal("new_entity", entities[entities.size() - 1])
return entities[entities.size() - 1]
func cost(point1: Vector2i, point2: Vector2i):
var cost = 1
point1 = get_real_coordinates(point1)
point2 = get_real_coordinates(point2)
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
func vec_distance(start: Vector2, end: Vector2):
var dx = end.x - start.x
var dy = end.y - start.y
if dx > Global.world.width / 2:
dx -= Global.world.width
elif dx < -Global.world.width / 2:
dx += Global.world.width
if dy > Global.world.height / 2:
dy -= Global.world.height
elif dy < 0 and dy < -Global.world.height / 2:
dy += Global.world.height
return Vector2i(dx, dy)
func get_custom_class(name: String):
for custom_class in ProjectSettings.get_global_class_list():
if custom_class["class"] == name:
return custom_class["path"]
pass