This repository has been archived on 2020-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
odyssey-old/Scenes/Map.gd

221 lines
6.4 KiB
GDScript3
Raw Normal View History

2020-07-07 08:38:32 +00:00
tool
2020-07-06 12:38:05 +00:00
extends Node2D
2020-07-07 12:51:30 +00:00
class_name GameMap
2020-07-06 12:38:05 +00:00
2020-07-06 22:00:39 +00:00
var ship_direction = 0
2020-07-10 00:09:54 +00:00
var ship_speed = 0
2020-07-06 17:23:42 +00:00
2020-07-13 00:20:09 +00:00
var current_ship_position = Vector2.ZERO
var current_ship_target = Vector2.ZERO
2020-07-06 22:00:39 +00:00
var current_ship_direction = 0
var current_ship_speed = 0
puppet var pup_ship_position = Vector2.ZERO
puppet var pup_ship_target = Vector2.ZERO
puppet var pup_ship_direction = 0
puppet var pup_ship_speed = 0
const MAX_ACCELERATION = 0.03
const DIRECTION_EASE = 0.4
2020-07-06 22:00:39 +00:00
const EPSILON = 0.01
const SCROLL_MULTIPLIER = 1e4
const ENGINE_MULTIPLIER = 5000
const MAX_SPEED = 0.2 # in Sectors/Second
const SLOW_SPEED = 0.02
const SLOW_SPEED_THRESHOLD = 0.2
const NO_SPEED_THRESHOLD = 0.1
2020-07-06 22:00:39 +00:00
2020-07-08 12:30:46 +00:00
const ProbeElectricity = preload("res://Actors/Systems/Electricity/ElectricProbe.tscn")
onready var tilemaps = [ $base, $cables, $floor, $walls ]
2020-07-06 17:23:42 +00:00
onready var pois = $pois
2020-07-07 08:38:32 +00:00
export var unlit = false setget set_unlit
onready var darkness = $darkness
2020-07-08 12:30:46 +00:00
export var shadow_intensity = 0.2
2020-07-07 08:38:32 +00:00
func _ready():
2020-07-07 11:01:12 +00:00
if Engine.editor_hint:
return
$editor.queue_free()
set_unlit(false)
if not is_network_master():
return
# Run autotile conversions and generate occlusions
$walls.run_conversions()
2020-07-08 12:30:46 +00:00
# Electricity setup
make_electric_probes($cables, "Wire")
# Set engines to expected power level
set_engine_strength(current_ship_speed)
2020-07-07 08:38:32 +00:00
2020-07-08 12:30:46 +00:00
func _process(delta: float):
2020-07-07 08:38:32 +00:00
if Engine.editor_hint:
return
var current_speed_ratio = current_ship_speed / MAX_SPEED
2020-07-07 08:38:32 +00:00
2020-07-08 12:30:46 +00:00
# Ease ship speed/direction changes
2020-07-06 22:00:39 +00:00
if abs(ship_direction - current_ship_direction) < EPSILON:
current_ship_direction = ship_direction
else:
current_ship_direction = lerp(current_ship_direction, ship_direction, delta * current_speed_ratio * DIRECTION_EASE)
var speed_delta = ship_speed - current_ship_speed
if abs(speed_delta) < EPSILON:
2020-07-06 22:00:39 +00:00
current_ship_speed = ship_speed
else:
current_ship_speed += MAX_ACCELERATION * max(0.1, 1-ease(current_speed_ratio, 0.6)) * delta * sign(speed_delta)
set_engine_strength(current_ship_speed * ENGINE_MULTIPLIER)
$deepspace.rotation = -current_ship_direction-PI/2
$deepspace.region_rect.position += Vector2(-sin(current_ship_direction), cos(current_ship_direction)) * current_ship_speed * SCROLL_MULTIPLIER * delta
func _physics_process(delta):
if Engine.editor_hint:
return
if is_network_master():
var distance = current_ship_position.distance_to(current_ship_target)
if distance > NO_SPEED_THRESHOLD:
ship_direction = current_ship_target.angle_to_point(current_ship_position)
if distance < SLOW_SPEED_THRESHOLD:
ship_speed = SLOW_SPEED
elif current_ship_speed == 0 or distance / current_ship_speed > ease(current_ship_speed, 2.8)*4000:
ship_speed = MAX_SPEED
else:
ship_speed = 0
rset("pup_ship_direction", ship_direction)
rset("pup_ship_speed", ship_speed)
else:
ship_speed = 0
rset("pup_ship_speed", ship_speed)
if current_ship_speed > EPSILON:
current_ship_position += (Vector2.RIGHT * current_ship_speed * delta).rotated(current_ship_direction)
rset("pup_ship_position", current_ship_position)
else:
ship_direction = pup_ship_direction
ship_speed = pup_ship_speed
current_ship_position = pup_ship_position
2020-07-07 11:01:12 +00:00
# Serialization / Deserialization
func serialize() -> Dictionary:
# Get tilemap data
var tilemapdata = {}
for tilemap in tilemaps:
var data = []
for cell in tilemap.get_used_cells():
data.append([cell, tilemap.get_cellv(cell)])
tilemapdata[tilemap.name] = data
# Get objects
var engines = serialize_children($engines)
var objects = serialize_children($objects)
var lights = serialize_children($lights)
var sockets = serialize_children($sockets)
var pois = serialize_children($pois)
return {
"tilemaps": tilemapdata,
"engines": engines,
"objects": objects,
"lights": lights,
"sockets": sockets,
"pois": pois,
"ship": {
"position": current_ship_position,
"target": current_ship_target,
"set_speed": ship_speed,
"set_direction": ship_direction,
"speed": current_ship_speed,
"direction": current_ship_direction
}
}
func deserialize(data: Dictionary) -> void:
# Create maps
for tilemap in data["tilemaps"]:
var tilemap_node = get_node(tilemap) as TileMap
for tile in data["tilemaps"][tilemap]:
tilemap_node.set_cellv(tile[0], tile[1])
tilemap_node.update_bitmask_region()
tilemap_node.update_dirty_quadrants()
# Create objects
deserialize_children($engines, data["engines"])
deserialize_children($objects, data["objects"])
deserialize_children($lights, data["lights"])
deserialize_children($pois, data["pois"])
deserialize_children($sockets, data["sockets"])
# Set ship parameters
current_ship_position = data["ship"]["position"]
current_ship_target = data["ship"]["target"]
ship_speed = data["ship"]["set_speed"]
ship_direction = data["ship"]["set_direction"]
current_ship_speed = data["ship"]["speed"]
current_ship_direction = data["ship"]["direction"]
# Run autotile conversions and generate occlusions
$walls.run_conversions()
func serialize_children(node: Node) -> Dictionary:
var data = {}
for child in node.get_children():
data[child.name] = {
"filename": child.filename,
"data": child.serialize(),
"transform": child.transform
}
return data
func deserialize_children(node: Node, data: Dictionary) -> void:
for node_name in data:
var node_data = data[node_name] as Dictionary
var node_scene = load(node_data.filename).instance()
node_scene.name = node_name
node_scene.transform = node_data.transform
node.add_child(node_scene, true)
node_scene.deserialize(node_data.data)
2020-07-08 12:30:46 +00:00
# Lighting
func set_unlit(val: bool):
unlit = val
if darkness:
darkness.visible = not val
# Engine related functions
2020-07-07 11:01:12 +00:00
func set_engine_strength(val: float):
# Set energy strength to current speed
for child in $engines.get_children():
if child is GameObjectEngine:
var engine = child as GameObjectEngine
engine.strength = val
2020-07-08 12:30:46 +00:00
# Tileset related functions
func make_electric_probes(tilemap: TileMap, tile_name: String):
var tile_id = tilemap.tile_set.find_tile_by_name(tile_name)
for cell in tilemap.get_used_cells_by_id(tile_id):
var coord = tilemap.map_to_world(cell)
var probe = ProbeElectricity.instance()
probe.position = coord
tilemap.add_child(probe)
func get_pois(type_filter, class_filter) -> Array:
var filtered = []
for child in $pois.get_children():
if type_filter != null and child.poitype != type_filter:
continue
if class_filter != null and child.poiclass != class_filter:
continue
filtered.append(child)
return filtered