Files
Diabolo/DoOtboy/do-otboy/scripts/apartment.gd
T

267 lines
12 KiB
GDScript

extends Node3D
var blockers: Array[Rect2] = []
var interactables: Dictionary = {}
var lamps: Array[OmniLight3D] = []
var windows: Array[MeshInstance3D] = []
var grid := AStarGrid2D.new()
const CELL := 0.25
const ORIGIN := Vector2(-6.5, -4.5)
func _ready() -> void:
build()
func material(color: Color, emission: float = 0.0) -> StandardMaterial3D:
var m := StandardMaterial3D.new()
m.albedo_color = color
m.roughness = 0.85
if emission > 0:
m.emission_enabled = true
m.emission = color
m.emission_energy_multiplier = emission
return m
func box(label: String, pos: Vector3, size: Vector3, color: Color, solid: bool = false, parent: Node = null) -> MeshInstance3D:
var m := MeshInstance3D.new()
m.name = label
var mesh := BoxMesh.new()
mesh.size = size
m.mesh = mesh
m.material_override = material(color)
if parent == null:
add_child(m)
else:
parent.add_child(m)
m.position = pos
if solid:
var body := StaticBody3D.new()
body.collision_layer = 1
body.collision_mask = 0
var shape := CollisionShape3D.new()
var bounds := BoxShape3D.new()
bounds.size = size
shape.shape = bounds
body.add_child(shape)
m.add_child(body)
blockers.append(Rect2(Vector2(pos.x-size.x/2, pos.z-size.z/2), Vector2(size.x,size.z)))
return m
func cylinder(pos: Vector3, radius: float, height: float, color: Color) -> MeshInstance3D:
var m := MeshInstance3D.new()
var mesh := CylinderMesh.new()
mesh.top_radius = radius
mesh.bottom_radius = radius
mesh.height = height
mesh.radial_segments = 20
m.mesh = mesh
m.material_override = material(color)
add_child(m)
m.position = pos
return m
func wall(pos: Vector3, size: Vector3) -> void:
box("Wall", pos, size, Color("657171"), true)
box("WallCap", pos + Vector3(0,size.y/2+0.018,0), Vector3(size.x+0.035,0.035,size.z+0.035), Color("8c9690"))
func floor_label(text: String, pos: Vector3) -> void:
var label := Label3D.new()
label.text = text
label.font_size = 44
label.pixel_size = 0.006
label.modulate = Color(0.8,0.84,0.78,0.34)
label.outline_size = 0
add_child(label)
label.position = pos
label.rotation_degrees.x = -90
func spot(id: String, title: String, pos: Vector3, approach: Vector3) -> void:
var body := StaticBody3D.new()
body.name = "Interact_" + id
body.set_meta("interaction", id)
body.collision_layer = 4
body.collision_mask = 0
var shape := CollisionShape3D.new()
var bounds := SphereShape3D.new()
bounds.radius = 0.45
shape.shape = bounds
body.add_child(shape)
add_child(body)
body.position = pos
var dot := cylinder(pos + Vector3(0,0.33,0),0.07,0.025,Color("e3b97a"))
dot.material_override = material(Color("f0c788"),0.5)
interactables[id] = {"title": title,"position":pos,"approach":approach,"marker":dot}
func build() -> void:
# A roofless apartment: raised cutaway walls leave every room readable.
box("Foundation",Vector3(0,-0.22,0),Vector3(13.6,0.4,9.6),Color("222f35"))
box("Floor",Vector3(0,-0.025,0),Vector3(13,0.08,9),Color("766752"))
for i in range(26):
for j in range(12):
var shade := 0.025 * sin(float(i*17+j*9))
box("Parquet",Vector3(-6.25+i*0.5,0.021,-4.125+j*0.75),Vector3(0.484,0.018,0.732),Color(0.40+shade,0.34+shade,0.265+shade))
# Bathroom tile overlay.
for i in range(10):
for j in range(7):
box("Tile",Vector3(1.75+i*0.5,0.045,1.1+j*0.5),Vector3(0.48,0.03,0.48),Color("6e8481") if (i+j)%2==0 else Color("617572"))
wall(Vector3(-6.55,0.65,0),Vector3(0.18,1.3,9.2))
wall(Vector3(6.55,0.65,0),Vector3(0.18,1.3,9.2))
wall(Vector3(0,0.65,-4.55),Vector3(13.2,1.3,0.18))
# Front wall kept low, entrance opening in the centre-left.
wall(Vector3(-4.6,0.28,4.55),Vector3(3.9,0.56,0.18))
wall(Vector3(2.6,0.28,4.55),Vector3(7.9,0.56,0.18))
wall(Vector3(1.35,0.65,-2.65),Vector3(0.16,1.3,3.8))
wall(Vector3(1.35,0.65,2.9),Vector3(0.16,1.3,3.3))
wall(Vector3(4.65,0.65,0.8),Vector3(3.8,1.3,0.16))
box("BedroomThreshold",Vector3(1.35,0.06,0.25),Vector3(0.25,0.04,1.6),Color("b9a17d"))
box("BathThreshold",Vector3(2.05,0.06,0.8),Vector3(1.2,0.04,0.25),Color("b9a17d"))
# Window frames and tape across glass.
for x in [-4.4,-1.9,3.9]:
var glass := box("Window",Vector3(x,1.0,-4.43),Vector3(1.65,0.9,0.035),Color("8ca9ac"))
glass.material_override = material(Color("a7c8cb"),0.7)
windows.append(glass)
box("WindowRail",Vector3(x,1.0,-4.39),Vector3(0.055,0.96,0.06),Color("bac0ae"))
var tape := box("Tape",Vector3(x,1.0,-4.36),Vector3(1.75,0.045,0.03),Color("c4bda0"))
tape.rotation_degrees.z = 27
var tape2 := box("Tape",Vector3(x,1.0,-4.35),Vector3(1.75,0.045,0.03),Color("c4bda0"))
tape2.rotation_degrees.z = -27
# Kitchen.
for x in [-5.65,-4.5,-3.35]:
box("Cabinet",Vector3(x,0.44,-3.85),Vector3(1.08,0.85,0.95),Color("627674"),true)
box("Counter",Vector3(x,0.91,-3.85),Vector3(1.14,0.08,1.0),Color("ada68c"))
box("Handle",Vector3(x,0.70,-3.35),Vector3(0.28,0.035,0.05),Color("283d40"))
box("Sink",Vector3(-4.5,0.96,-3.85),Vector3(0.67,0.03,0.60),Color("3c5559"))
box("Tap",Vector3(-4.5,1.12,-4.12),Vector3(0.06,0.30,0.06),Color("bbc0b5"))
box("Stove",Vector3(-3.35,0.97,-3.85),Vector3(0.85,0.03,0.73),Color("29373c"))
for x in [-3.56,-3.16]:
for z in [-4.04,-3.67]:
cylinder(Vector3(x,1.0,z),0.13,0.025,Color("121f26"))
box("Fridge",Vector3(-1.9,0.83,-3.9),Vector3(1.0,1.65,0.95),Color("b4b8a3"),true)
box("FridgeHandle",Vector3(-1.55,0.9,-3.39),Vector3(0.04,0.4,0.06),Color("485655"))
# Dining table, computer and papers.
box("Table",Vector3(-2.3,0.75,-1.5),Vector3(2.0,0.12,1.15),Color("927752"),true)
for x in [-3.1,-1.5]:
for z in [-1.9,-1.1]:
box("TableLeg",Vector3(x,0.35,z),Vector3(0.09,0.7,0.09),Color("354747"))
box("LaptopBase",Vector3(-2.4,0.85,-1.5),Vector3(0.65,0.05,0.45),Color("243740"))
var screen := box("LaptopScreen",Vector3(-2.4,1.08,-1.70),Vector3(0.66,0.45,0.04),Color("66b8bb"))
screen.material_override = material(Color("6bb4b2"),0.5)
box("Paper",Vector3(-1.65,0.84,-1.5),Vector3(0.30,0.012,0.42),Color("d6cdb2"))
box("Chair",Vector3(-2.2,0.30,-0.5),Vector3(0.55,0.55,0.45),Color("4b6561"),true)
# Living room.
box("Rug",Vector3(-3.5,0.05,1.65),Vector3(4.3,0.025,3.1),Color("425d60"))
for z in [0.25,3.05]:
box("RugStripe",Vector3(-3.5,0.07,z),Vector3(4.0,0.01,0.07),Color("788581"))
box("SofaBase",Vector3(-5.4,0.30,1.75),Vector3(1.35,0.55,2.85),Color("5d7062"),true)
box("SofaBack",Vector3(-5.98,0.68,1.75),Vector3(0.23,0.95,2.85),Color("53665a"))
for z in [0.68,1.75,2.82]:
box("Cushion",Vector3(-5.35,0.64,z),Vector3(0.93,0.17,0.83),Color("78816b"))
box("CoffeeTable",Vector3(-3.35,0.36,1.8),Vector3(1.5,0.65,0.8),Color("8f7758"),true)
cylinder(Vector3(-3.0,0.72,1.9),0.10,0.16,Color("c4bda6"))
box("Book",Vector3(-3.65,0.72,1.8),Vector3(0.30,0.06,0.42),Color("925e4f"))
box("PhoneTable",Vector3(-5.7,0.48,-0.35),Vector3(0.75,0.9,0.65),Color("8c765a"),true)
var phone := box("Phone",Vector3(-5.7,0.97,-0.35),Vector3(0.20,0.045,0.35),Color("8abfb2"))
phone.material_override = material(Color("90cbbb"),0.5)
box("RadioShelf",Vector3(-5.95,0.50,-1.85),Vector3(0.6,1.0,1.05),Color("71664e"),true)
box("Radio",Vector3(-5.95,1.13,-1.85),Vector3(0.52,0.25,0.65),Color("3a4c4e"))
box("RadioDial",Vector3(-5.64,1.16,-1.85),Vector3(0.025,0.07,0.4),Color("dbbf85"))
# Bedroom / mother's room.
box("BedBase",Vector3(4.5,0.30,-2.1),Vector3(2.3,0.5,3.5),Color("4d5750"),true)
box("Mattress",Vector3(4.5,0.65,-2.1),Vector3(2.2,0.28,3.4),Color("b4b5a0"))
box("Blanket",Vector3(4.5,0.83,-1.60),Vector3(2.24,0.12,2.25),Color("7b8580"))
box("Pillow",Vector3(4.5,0.85,-3.25),Vector3(1.5,0.18,0.65),Color("d4cfb7"))
box("Cabinet",Vector3(6.0,0.72,-3.0),Vector3(0.62,1.4,1.3),Color("71644e"),true)
box("Powerbank",Vector3(6.0,1.46,-3.0),Vector3(0.25,0.08,0.42),Color("d8b47b"))
box("Concentrator",Vector3(2.25,0.38,-3.2),Vector3(0.62,0.74,0.65),Color("b7bcb0"),true)
cylinder(Vector3(2.65,0.45,-3.7),0.13,0.9,Color("527c7c"))
box("PhotoFrame",Vector3(2.3,0.82,-3.2),Vector3(0.25,0.22,0.06),Color("8b614d"))
# Bathroom, sheltered by internal walls.
box("Bath",Vector3(5.65,0.32,2.6),Vector3(1.2,0.6,2.7),Color("c6cbb9"),true)
box("BathInside",Vector3(5.65,0.64,2.6),Vector3(0.90,0.025,2.34),Color("698883"))
box("Basin",Vector3(3.6,0.55,3.9),Vector3(1.15,1.0,0.65),Color("c2c5af"),true)
box("Water",Vector3(3.6,1.08,3.9),Vector3(0.75,0.025,0.45),Color("6d9495"))
box("Towel",Vector3(6.4,0.8,1.2),Vector3(0.03,0.48,0.55),Color("9c9474"))
# Entrance and fuse box.
box("Door",Vector3(-1.7,0.65,4.48),Vector3(1.5,1.3,0.12),Color("6a5140"))
box("Intercom",Vector3(-0.25,0.90,4.35),Vector3(0.25,0.45,0.16),Color("c2bca5"))
box("FuseBox",Vector3(0.55,0.95,4.35),Vector3(0.5,0.60,0.16),Color("546f70"))
box("Shoes",Vector3(-1.9,0.14,3.65),Vector3(0.55,0.25,0.45),Color("29383c"),true)
box("Mat",Vector3(-1.7,0.055,3.75),Vector3(1.7,0.02,1.1),Color("555c4c"))
# Interaction points include reachable floor approaches.
spot("phone","Телефон",Vector3(-5.7,1.03,-0.35),Vector3(-4.8,0,-0.35))
spot("laptop","Рабочий ноутбук",Vector3(-2.4,1.15,-1.5),Vector3(-0.8,0,-1.5))
spot("radio","Радиоприёмник",Vector3(-5.95,1.35,-1.85),Vector3(-5.0,0,-1.85))
spot("drawer","Кухонный ящик",Vector3(-5.65,1.05,-3.6),Vector3(-4.7,0,-2.6))
spot("mother","Мамины вещи",Vector3(2.25,0.9,-3.2),Vector3(2.3,0,-2.1))
spot("battery","Запасной аккумулятор",Vector3(6.0,1.5,-3.0),Vector3(6.0,0,-1.8))
spot("intercom","Домофон",Vector3(-0.25,1.1,4.25),Vector3(-0.25,0,3.5))
spot("fuse","Электрощиток",Vector3(0.55,1.2,4.2),Vector3(0.55,0,3.4))
spot("door","Входная дверь",Vector3(-1.7,1.0,4.35),Vector3(-1.0,0,3.3))
spot("map","План района",Vector3(-6.35,0.9,3.5),Vector3(-5.1,0,3.7))
box("Map",Vector3(-6.39,0.8,3.55),Vector3(0.04,0.7,1.1),Color("c2b99c"))
spot("bath","Внутренняя стена",Vector3(2.0,0.3,2.8),Vector3(2.3,0,2.8))
floor_label("КУХНЯ",Vector3(-4.3,0.09,-2.6))
floor_label("ГОСТИНАЯ",Vector3(-2.8,0.09,0.35))
floor_label("МАМИНА КОМНАТА",Vector3(4.0,0.09,-0.1))
floor_label("ВАННАЯ",Vector3(3.9,0.09,1.55))
floor_label("ПРИХОЖАЯ",Vector3(-0.6,0.09,2.5))
# Pools of domestic light; the city outside stays cold.
add_lamp(Vector3(-3.7,3,0.4),Color("ffd19a"),1.3,6)
add_lamp(Vector3(4.3,3,-1.8),Color("b9c7b0"),0.9,5)
add_lamp(Vector3(3.6,2.5,2.7),Color("a2c4be"),0.8,4)
setup_grid()
func add_lamp(pos: Vector3, color: Color, energy: float, radius: float) -> void:
var light := OmniLight3D.new()
add_child(light)
light.position = pos
light.light_color = color
light.light_energy = energy
light.omni_range = radius
light.shadow_enabled = false
light.set_meta("normal_energy", energy)
lamps.append(light)
func set_power(enabled: bool) -> void:
for light in lamps:
light.light_energy = float(light.get_meta("normal_energy")) * (1.0 if enabled else 0.12)
func setup_grid() -> void:
grid.region = Rect2i(0,0,52,36)
grid.cell_size = Vector2(CELL,CELL)
grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES
grid.update()
for x in range(52):
for y in range(36):
var point := ORIGIN + Vector2(x+0.5,y+0.5)*CELL
var blocked := x==0 or x==51 or y==0 or y==35
for rect in blockers:
if rect.grow(0.25).has_point(point):
blocked = true
break
grid.set_point_solid(Vector2i(x,y),blocked)
func cell_of(pos: Vector3) -> Vector2i:
return Vector2i(clampi(int(floor((pos.x-ORIGIN.x)/CELL)),0,51),clampi(int(floor((pos.z-ORIGIN.y)/CELL)),0,35))
func world_of(cell: Vector2i) -> Vector3:
return Vector3(ORIGIN.x+(cell.x+0.5)*CELL,0,ORIGIN.y+(cell.y+0.5)*CELL)
func nearest_cell(pos: Vector3) -> Vector2i:
var cell := cell_of(pos)
if not grid.is_point_solid(cell):
return cell
for radius in range(1,9):
for x in range(-radius,radius+1):
for y in range(-radius,radius+1):
var test := cell + Vector2i(x,y)
if grid.is_in_boundsv(test) and not grid.is_point_solid(test):
return test
return cell
func path(from: Vector3, to: Vector3) -> Array[Vector3]:
var result: Array[Vector3] = []
var cells := grid.get_id_path(nearest_cell(from),nearest_cell(to))
for cell in cells:
result.append(world_of(cell))
return result