Skip to content

Commit

Permalink
Scriptable environment setting
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrw committed Mar 25, 2024
1 parent cfe0b9c commit 6995c6f
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 61 deletions.
29 changes: 29 additions & 0 deletions app/environments/opposite.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[gd_resource type="Environment" load_steps=3 format=2]

[ext_resource path="res://textures/planet_sky.png" type="Texture" id=1]

[sub_resource type="PanoramaSky" id=1]
panorama = ExtResource( 1 )

[resource]
background_mode = 2
background_sky = SubResource( 1 )
background_sky_orientation = Basis( -1, 1.52079e-08, -8.60899e-08, 0, 0.984753, 0.173958, 8.74228e-08, 0.173958, -0.984753 )
background_energy = 0.5
ambient_light_color = Color( 0.741176, 0.996078, 0.588235, 1 )
ambient_light_energy = 1.5
ambient_light_sky_contribution = 0.0
fog_enabled = true
fog_color = Color( 0, 0, 0, 1 )
fog_sun_color = Color( 0.211765, 0.211765, 0.211765, 1 )
fog_depth_begin = 150.0
fog_depth_end = 200.0
fog_height_max = 50.0
tonemap_mode = 3
tonemap_white = 0.13
dof_blur_far_distance = 200.0
dof_blur_far_transition = 20.0
dof_blur_far_quality = 0
glow_enabled = true
glow_bloom = 1.0
glow_high_quality = true
30 changes: 30 additions & 0 deletions app/environments/wild_imagination.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[gd_resource type="Environment" load_steps=3 format=2]

[ext_resource path="res://textures/planet_sky.png" type="Texture" id=1]

[sub_resource type="PanoramaSky" id=1]
panorama = ExtResource( 1 )

[resource]
background_mode = 2
background_sky = SubResource( 1 )
background_sky_orientation = Basis( -1, 1.52079e-08, -8.60899e-08, 0, 0.984753, 0.173958, 8.74228e-08, 0.173958, -0.984753 )
background_energy = 0.5
ambient_light_color = Color( 0.0509804, 0.858824, 0.843137, 1 )
ambient_light_energy = 11.36
fog_enabled = true
fog_color = Color( 0, 0, 0, 1 )
fog_sun_color = Color( 0.211765, 0.211765, 0.211765, 1 )
fog_depth_begin = 150.0
fog_depth_end = 200.0
fog_height_max = 50.0
tonemap_mode = 3
tonemap_white = 0.13
dof_blur_far_distance = 200.0
dof_blur_far_transition = 20.0
dof_blur_far_quality = 0
glow_enabled = true
glow_bloom = 1.0
glow_high_quality = true
adjustment_enabled = true
adjustment_saturation = 5.42
14 changes: 13 additions & 1 deletion src/controllers/script_controllers/host_bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,15 @@ proc open_sign(self: Unit): Sign =
proc `open_sign=`(self: Unit, value: Sign) =
state.open_sign = value

# World bindings

proc environment(_: PNode): string =
state.config.environment

proc `environment=`(_: PNode, mode: string) =
state.config_value.value:
environment = mode

# Sign bindings

proc new_markdown_sign(
Expand Down Expand Up @@ -583,4 +592,7 @@ proc bridge_to_vm*(worker: Worker) =

result.bridged_from_vm "players",
playing, `playing=`, god, `god=`, flying, `flying=`, tool, `tool=`, coding,
`coding=`, running, `running=`, open_sign, `open_sign=`
`coding=`, running, `running=`, open_sign, `open_sign=`, environment,
`environment=`

result.bridged_from_vm "worlds", environment, `environment=`
39 changes: 25 additions & 14 deletions src/game.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ const savable_flags = {
ConsoleVisible, MouseCaptured, Flying, God, AltWalkSpeed, AltFlySpeed
}

const game_modes = [
const environment_names = [
"default", "blue", "bright", "bw", "bw2", "bw3", "noir", "strange",
"dream_mode"
"dream_mode", "opposite", "none", "wild_imagination"
]

var environments {.threadvar.}: Table[string, Environment]
var saved_transform {.threadvar.}: Transform
var saved_rotation {.threadvar.}: float
var saved_flags {.threadvar.}: set[LocalStateFlags]
Expand All @@ -49,7 +50,6 @@ gdobj Game of Node:
force_quit_at = MonoTime.high
node_controller: NodeController
script_controller: ScriptController
game_mode: int

method process*(delta: float) =
Zen.thread_ctx.boop
Expand Down Expand Up @@ -189,6 +189,7 @@ gdobj Game of Node:
mouse_sensitivity = uc.mouse_sensitivity ||= 5.0
gamepad_sensitivity = uc.gamepad_sensitivity ||= 2.5
invert_gamepad_y_axis = uc.invert_gamepad_y_axis ||= false
environment = "default"

state.set_flag(God, uc.god_mode ||= false)

Expand Down Expand Up @@ -260,6 +261,22 @@ gdobj Game of Node:
self.stats = self.find_node("stats").as(Label)
self.stats.visible = state.config.show_stats

state.config_value.changes:
if removed and change.item.environment != state.config.environment:
let env =
state.nodes.game.find_node("Level").get_node("WorldEnvironment") as
WorldEnvironment
if state.config.environment notin environments:
let res = \"res://environments/{state.config.environment}.tres"

let environment = load(res) as Environment
if not ?environment:
logger("err", \"Environment {state.config.environment} not found")
return
environments[state.config.environment] = environment
env.environment = environments[state.config.environment]
logger("info", \"Changed game mode to {state.config.environment}")

state.player_value.changes:
if added and ?change.item and restarting:
change.item.transform = saved_transform
Expand Down Expand Up @@ -420,17 +437,11 @@ gdobj Game of Node:
if host_os != "macosx":
state.push_flag Quitting
elif event.is_action_pressed("change_mode"):
self.game_mode += 1
if self.game_mode >= game_modes.len:
self.game_mode = 0
let env =
state.nodes.game.find_node("Level").get_node("WorldEnvironment") as
WorldEnvironment
let mode_scene =
load(\"res://environments/{game_modes[self.game_mode]}.tres") as
Environment
env.environment = mode_scene
logger("info", \"Changed game mode to {game_modes[self.game_mode]}")
var mode = state.config.environment
while (mode = environment_names.sample; mode == state.config.environment):
discard
state.config_value.value:
environment = mode
elif EditorVisible notin state.local_flags:
if event.is_action_pressed("toggle_mouse_captured"):
state.set_flag MouseCaptured, MouseCaptured notin state.local_flags
Expand Down
4 changes: 2 additions & 2 deletions src/models/bot_code_template.nim.nimf
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#proc bot_code_template(base64_code, file_name, imports: string): string =
import system except echo, quit
import std / [strutils]
import types, class_macros, players, state_machine, base_api, bots, builds,
signs, bots_private
import types, class_macros, worlds, players, state_machine, base_api, bots,
builds, signs, bots_private

let instance_global_by_default = true
var move_mode = 1
Expand Down
4 changes: 2 additions & 2 deletions src/models/build_code_template.nim.nimf
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#proc build_code_template(base64_code, file_name, imports: string): string =
import system except echo, quit
import std / [strutils]
import types, class_macros, players, state_machine, base_api, bots, builds,
signs, builds_private
import types, class_macros, players, worlds, state_machine, base_api, bots,
builds, signs, builds_private

let instance_global_by_default = false
var move_mode = 1
Expand Down
1 change: 1 addition & 0 deletions src/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type
dock_icon_size*: float
show_stats*: bool
mega_pixels*: float
environment*: string
world_dir*: string
level_dir*: string
data_dir*: string
Expand Down
1 change: 0 additions & 1 deletion vmlib/enu/base_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ proc lean*(self: Unit, degrees: float, move_mode: int) =
if degrees <= 180:
self.lean Directions.right, degrees, move_mode
else:
let d = 180 - (degrees - 180)
self.lean Directions.left, 180 - (degrees - 180), move_mode

template lean*(self: Unit, direction: Directions, degrees = 90.0) =
Expand Down
55 changes: 14 additions & 41 deletions vmlib/enu/players.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,17 @@ let player* = PlayerType()
register_active(player)

bridged_to_host:
proc tool*(self: PlayerType): Tools =
discard

proc `tool=`*(self: PlayerType, value: Tools) =
discard

proc playing*(self: PlayerType): bool =
discard

proc `playing=`*(self: PlayerType, value: bool) =
discard

proc flying*(self: PlayerType): bool =
discard

proc `flying=`*(self: PlayerType, value: bool) =
discard

proc running*(self: PlayerType): bool =
discard

proc `running=`*(self: PlayerType, value: bool) =
discard

proc god*(self: PlayerType): bool =
discard

proc `god=`*(self: PlayerType, value: bool) =
discard

proc coding*(self: PlayerType): Unit =
discard

proc `coding=`*(self: PlayerType, value: Unit) =
discard

proc open_sign*(self: PlayerType): Sign =
discard

proc `open_sign=`*(self: PlayerType, value: Sign) =
discard
proc tool*(self: PlayerType): Tools
proc `tool=`*(self: PlayerType, value: Tools)
proc playing*(self: PlayerType): bool
proc `playing=`*(self: PlayerType, value: bool)
proc flying*(self: PlayerType): bool
proc `flying=`*(self: PlayerType, value: bool)
proc running*(self: PlayerType): bool
proc `running=`*(self: PlayerType, value: bool)
proc god*(self: PlayerType): bool
proc `god=`*(self: PlayerType, value: bool)
proc coding*(self: PlayerType): Unit
proc `coding=`*(self: PlayerType, value: Unit)
proc open_sign*(self: PlayerType): Sign
proc `open_sign=`*(self: PlayerType, value: Sign)
2 changes: 2 additions & 0 deletions vmlib/enu/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type
seed: int
sign*: Sign

World* = ref object of RootObj

PositionOffset* = object
position*: Vector3
offset*: float
Expand Down
8 changes: 8 additions & 0 deletions vmlib/enu/worlds.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import system except echo
import types, vm_bridge_utils

var world* = World()

bridged_to_host:
proc environment*(self: World): string
proc `environment=`*(self: World, value: string)

0 comments on commit 6995c6f

Please sign in to comment.