Skip to content

Commit

Permalink
pal4: implement wait, flash_in_black, flash_out_black
Browse files Browse the repository at this point in the history
  • Loading branch information
dontpanic92 committed Apr 1, 2024
1 parent 1ec67d3 commit 9bc34cd
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 41 deletions.
50 changes: 50 additions & 0 deletions radiance/radiance/src/utils/act_drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use imgui::Ui;

use super::interp_value::InterpValue;

pub struct ActDrop {
darkness: InterpValue<f32>,
}

impl ActDrop {
pub fn new() -> Self {
ActDrop {
darkness: InterpValue::new(0., 0., 0.),
}
}

pub fn set_darkness(&mut self, darkness: InterpValue<f32>) {
self.darkness = darkness;
}

pub fn current(&self) -> f32 {
self.darkness.value()
}

pub fn update(&mut self, ui: &Ui, delta_sec: f32) {
self.darkness.update(delta_sec);

let value = self.darkness.value();
if value == 0. {
return;
}

let [width, height] = ui.io().display_size;
let color = [0., 0., 0., value];
let style = ui.push_style_color(imgui::StyleColor::WindowBg, color);
ui.window("actdrop")
.position([0., 0.], imgui::Condition::Always)
.size([width, height], imgui::Condition::Always)
.movable(false)
.resizable(false)
.collapsible(false)
.title_bar(false)
.draw_background(true)
.scroll_bar(false)
.nav_focus(false)
.focused(false)
.mouse_inputs(false)
.build(|| {});
style.pop();
}
}
1 change: 1 addition & 0 deletions radiance/radiance/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod act_drop;
pub mod free_view;
pub mod interp_value;

Expand Down
15 changes: 15 additions & 0 deletions yaobow/shared/src/openpal4/app_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use radiance::{
math::Vec3,
radiance::{TaskHandle, TaskManager, UiManager},
rendering::{ComponentFactory, VideoPlayer},
utils::{act_drop::ActDrop, interp_value::InterpValue},
};

use crate::ui::dialog_box::{AvatarPosition, DialogBox};
Expand All @@ -30,6 +31,7 @@ pub struct Pal4AppContext {
bgm_task: Option<Rc<TaskHandle>>,
sound_tasks: HashMap<i32, Rc<TaskHandle>>,
sound_id: i32,
actdrop: ActDrop,
voice_task: Option<Rc<TaskHandle>>,
camera_data: Option<CameraDataFile>,
scene_name: String,
Expand Down Expand Up @@ -59,6 +61,7 @@ impl Pal4AppContext {
bgm_task: None,
sound_tasks: HashMap::new(),
sound_id: 0,
actdrop: ActDrop::new(),
voice_task: None,
camera_data: None,
scene_name: String::new(),
Expand All @@ -69,6 +72,18 @@ impl Pal4AppContext {
}
}

pub fn update(&mut self, delta_sec: f32) {
self.actdrop.update(self.ui.ui(), delta_sec);
}

pub fn set_actdrop(&mut self, darkness: InterpValue<f32>) {
self.actdrop.set_darkness(darkness);
}

pub fn get_actdrop(&self) -> &ActDrop {
&self.actdrop
}

pub fn set_leader(&mut self, leader: i32) {
self.leader = leader as usize;
self.scene.get_player(self.leader).set_visible(true);
Expand Down
1 change: 1 addition & 0 deletions yaobow/shared/src/openpal4/director.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl IDirectorImpl for OpenPAL4Director {
_ui: &imgui::Ui,
delta_sec: f32,
) -> Option<crosscom::ComRc<radiance::comdef::IDirector>> {
self.vm.borrow_mut().app_context_mut().update(delta_sec);
self.vm.borrow_mut().execute(delta_sec);

None
Expand Down
51 changes: 44 additions & 7 deletions yaobow/shared/src/openpal4/scripting.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cell::RefCell, rc::Rc};

use imgui::MouseButton;
use radiance::{input::Key, math::Vec3, video::VideoStreamState};
use radiance::{input::Key, math::Vec3, utils::interp_value::InterpValue, video::VideoStreamState};

use crate::{
as_params,
Expand Down Expand Up @@ -1987,8 +1987,17 @@ fn script_music_resume(_: &str, _vm: &mut ScriptVm<Pal4AppContext>) -> Pal4Funct
}

fn wait(_: &str, vm: &mut ScriptVm<Pal4AppContext>) -> Pal4FunctionState {
as_params!(vm, _time: f64);
Pal4FunctionState::Completed
as_params!(vm, time: f64);

let mut time = time as f32;
Pal4FunctionState::Yield(Box::new(move |vm, delta_sec| {
if time <= 0.0 {
ContinuationState::Completed
} else {
time = time - delta_sec;
ContinuationState::Loop
}
}))
}

fn talk(_: &str, vm: &mut ScriptVm<Pal4AppContext>) -> Pal4FunctionState {
Expand Down Expand Up @@ -2313,13 +2322,41 @@ fn camera_wait(_: &str, _vm: &mut ScriptVm<Pal4AppContext>) -> Pal4FunctionState
}

fn flash_out_black(_: &str, vm: &mut ScriptVm<Pal4AppContext>) -> Pal4FunctionState {
as_params!(vm, _duration: f32, _keep: i32, _sync: i32);
Pal4FunctionState::Completed
as_params!(vm, duration: f32, keep: i32, sync: i32);

vm.app_context
.set_actdrop(InterpValue::new(0., 1., duration));

if sync == 1 {
Pal4FunctionState::Yield(Box::new(move |vm, _| {
if vm.app_context.get_actdrop().current() == 1. {
ContinuationState::Completed
} else {
ContinuationState::Loop
}
}))
} else {
Pal4FunctionState::Completed
}
}

fn flash_in_black(_: &str, vm: &mut ScriptVm<Pal4AppContext>) -> Pal4FunctionState {
as_params!(vm, _duration: f32, _sync: i32);
Pal4FunctionState::Completed
as_params!(vm, duration: f32, sync: i32);

vm.app_context
.set_actdrop(InterpValue::new(1., 0., duration));

if sync == 1 {
Pal4FunctionState::Yield(Box::new(move |vm, _| {
if vm.app_context.get_actdrop().current() == 0. {
ContinuationState::Completed
} else {
ContinuationState::Loop
}
}))
} else {
Pal4FunctionState::Completed
}
}

fn flash_out_white(_: &str, vm: &mut ScriptVm<Pal4AppContext>) -> Pal4FunctionState {
Expand Down
42 changes: 8 additions & 34 deletions yaobow/shared/src/openswd5/scripting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use radiance::{
audio::{AudioEngine, AudioMemorySource, AudioSourceState, Codec},
comdef::ISceneManager,
input::{InputEngine, Key},
math::Vec3,
radiance::UiManager,
rendering::{ComponentFactory, Sprite, VideoPlayer},
utils::interp_value::InterpValue,
utils::{act_drop::ActDrop, interp_value::InterpValue},
};

use crate::scripting::lua50_32::Lua5032Vm;
Expand All @@ -36,7 +35,7 @@ pub struct SWD5Context {
talk_msg: Option<TalkMsg>,

movie_texture: Option<TextureId>,
darkness: InterpValue<f32>,
actdrop: ActDrop,
anykey_down: bool,
}

Expand Down Expand Up @@ -66,7 +65,7 @@ impl SWD5Context {
story_pic: None,
talk_msg: None,
movie_texture: None,
darkness: InterpValue::new(0., 0., 0.),
actdrop: ActDrop::new(),
anykey_down: false,
}
}
Expand Down Expand Up @@ -95,42 +94,15 @@ impl SWD5Context {
self.anykey_down = self.anykey_down || self.anykey_down();
}

self.darkness.update(delta_sec);
self.actdrop.update(self.ui.ui(), delta_sec);

self.update_audio();
self.update_actdrop();
self.update_story_pic();
self.update_storymsg();
self.update_talkmsg();
self.update_video();
}

fn update_actdrop(&mut self) {
let value = self.darkness.value();
if value == 0. {
return;
}

let ui = self.ui.ui();
let [width, height] = ui.io().display_size;
let color = [0., 0., 0., value];
let style = ui.push_style_color(imgui::StyleColor::WindowBg, color);
ui.window("actdrop")
.position([0., 0.], imgui::Condition::Always)
.size([width, height], imgui::Condition::Always)
.movable(false)
.resizable(false)
.collapsible(false)
.title_bar(false)
.draw_background(true)
.scroll_bar(false)
.nav_focus(false)
.focused(false)
.mouse_inputs(false)
.build(|| {});
style.pop();
}

fn update_storymsg(&mut self) {
if self.anykey_down() {
self.story_msg = None;
Expand Down Expand Up @@ -269,11 +241,13 @@ impl SWD5Context {
fn lock_player(&mut self, f: f64) {}

fn dark(&mut self, speed: f64) {
self.darkness = InterpValue::new(0., 1., 0.1 * speed as f32);
self.actdrop
.set_darkness(InterpValue::new(0., 1., 0.1 * speed as f32));
}

fn undark(&mut self, speed: f64) {
self.darkness = InterpValue::new(1., 0., 0.1 * speed as f32);
self.actdrop
.set_darkness(InterpValue::new(1., 0., 0.1 * speed as f32));
}

fn chang_map(&mut self, map_id: f64, x: f64, y: f64, z: f64) {
Expand Down
4 changes: 4 additions & 0 deletions yaobow/shared/src/scripting/angelscript/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ impl<TAppContext: 'static> ScriptVm<TAppContext> {
&self.app_context
}

pub fn app_context_mut(&mut self) -> &mut TAppContext {
&mut self.app_context
}

pub fn set_function(&mut self, module: Rc<RefCell<ScriptModule>>, index: usize) {
self.call_stack.push(self.context.clone().unwrap());
self.context = Some(ScriptFunctionContext::new(module, index));
Expand Down

0 comments on commit 9bc34cd

Please sign in to comment.