Galactica/content/ui/flying.rhai

331 lines
6.9 KiB
Plaintext
Raw Normal View History

2024-02-03 11:24:17 -08:00
fn init(state) {
2024-02-04 11:45:49 -08:00
2024-02-05 10:12:15 -08:00
conf::show_starfield(true);
conf::show_phys(true);
2024-02-04 11:45:49 -08:00
2024-02-05 10:12:15 -08:00
sprite::add(
2024-02-03 07:33:10 -08:00
"ring",
"ui::status",
Rect(
-5.0, -5.0, 100.0, 100.0,
2024-02-04 11:45:49 -08:00
Anchor::NorthEast,
Anchor::NorthEast
2024-02-03 07:33:10 -08:00
)
);
2024-02-05 10:12:15 -08:00
radialbar::add(
2024-02-03 07:33:10 -08:00
"shield", 2.5,
Color(0.3, 0.6, 0.8, 1.0),
Rect(
-9.5, -9.5, 91.0, 91.0,
2024-02-04 11:45:49 -08:00
Anchor::NorthEast,
Anchor::NorthEast
2024-02-03 07:33:10 -08:00
)
);
2024-02-05 10:12:15 -08:00
radialbar::add(
2024-02-03 07:33:10 -08:00
"hull", 2.5,
Color(0.8, 0.7, 0.5, 1.0),
Rect(
-13.5, -13.5, 83.0, 83.0,
2024-02-04 11:45:49 -08:00
Anchor::NorthEast,
Anchor::NorthEast
2024-02-03 07:33:10 -08:00
)
);
2024-02-04 22:41:26 -08:00
let radar_size = 150.0;
let radar_range = 4000.0;
2024-02-08 20:41:04 -08:00
let hide_range = 0.85;
let shrink_distance = 20.0;
2024-02-04 22:41:26 -08:00
2024-02-05 10:12:15 -08:00
sprite::add(
2024-02-04 22:41:26 -08:00
"radar",
"ui::radar",
Rect(
5.0, -5.0,
radar_size, radar_size,
Anchor::NorthWest,
Anchor::NorthWest
)
);
let init_pos = Rect(
(radar_size / 2.0 + 5),
(radar_size / -2.0 - 5),
3.5, 3.5,
2024-02-16 13:29:19 -08:00
Anchor::NorthWest,
Anchor::Center
2024-02-04 22:41:26 -08:00
);
2024-02-05 10:12:15 -08:00
sprite::add("radar.frame.ne", "ui::radarframe", init_pos);
sprite::add("radar.frame.se", "ui::radarframe", init_pos);
sprite::add("radar.frame.sw", "ui::radarframe", init_pos);
sprite::add("radar.frame.nw", "ui::radarframe", init_pos);
sprite::set_angle("radar.frame.se", 90.0);
sprite::set_angle("radar.frame.sw", 180.0);
sprite::set_angle("radar.frame.nw", 270.0);
sprite::set_color("radar.frame.ne", Color(0.3, 0.3, 0.3, 1.0));
sprite::set_color("radar.frame.se", Color(0.3, 0.3, 0.3, 1.0));
sprite::set_color("radar.frame.sw", Color(0.3, 0.3, 0.3, 1.0));
sprite::set_color("radar.frame.nw", Color(0.3, 0.3, 0.3, 1.0));
2024-02-04 22:41:26 -08:00
2024-02-05 10:12:15 -08:00
sprite::add("radar.arrow", "ui::centerarrow", init_pos);
2024-02-03 07:33:10 -08:00
}
2024-02-03 11:24:17 -08:00
fn event(state, event) {
if type_of(event) == "PlayerShipStateEvent" {
if state.player_ship().is_landed() {
2024-02-05 10:12:15 -08:00
ui::go_to_scene("landed");
2024-02-08 20:41:04 -08:00
return PlayerDirective::None;
2024-02-03 11:24:17 -08:00
}
2024-02-08 20:41:04 -08:00
return PlayerDirective::None;
2024-02-03 11:24:17 -08:00
}
2024-02-07 15:58:48 -08:00
if type_of(event) == "ScrollEvent" {
return ui::set_camera_zoom(
ui::get_camera_zoom()
- (5.0 * event.val())
);
}
if type_of(event) == "KeyboardEvent" {
if event.key() == "up" {
return PlayerDirective::Engine(event.is_down());
}
if event.key() == "left" {
return PlayerDirective::TurnLeft(event.is_down());
}
if event.key() == "right" {
return PlayerDirective::TurnRight(event.is_down());
}
if event.key() == "space" {
return PlayerDirective::Guns(event.is_down());
}
if event.key() == "L" && event.is_down() {
return PlayerDirective::Land;
}
2024-02-08 20:41:04 -08:00
return PlayerDirective::None;
2024-02-07 15:58:48 -08:00
}
2024-02-03 18:35:32 -08:00
}
2024-02-04 11:45:49 -08:00
fn step(state) {
2024-02-16 18:26:55 -08:00
if state.player_ship().stat_shield_strength() == 0.0 {
radialbar::set_val("shield", 0.0);
} else {
radialbar::set_val("shield",
state.player_ship().current_shields()
/ state.player_ship().stat_shield_strength()
);
}
2024-02-05 10:12:15 -08:00
radialbar::set_val("hull",
2024-02-16 13:29:19 -08:00
state.player_ship().current_hull()
/ state.player_ship().total_hull()
2024-02-03 18:35:32 -08:00
);
2024-02-04 22:41:26 -08:00
// TODO: share variables with init();
let p_pos = state.player_ship().get_pos();
// Radar arrow
{
let q = Vector(0.0, 0.0) - state.player_ship().get_pos();
let m = q.norm();
let angle = Vector(1.0, 0.0).clockwise_angle(q);
let position = Vector(5.0 + (radar_size / 2.0), -5.0 - (radar_size / 2.0));
let rot = Vector(0.915 * (radar_size / 2.0), 0.0);
let pos = position + rot.rotate(angle);
2024-02-05 10:12:15 -08:00
sprite::set_rect("radar.arrow",
2024-02-04 22:41:26 -08:00
Rect(
pos.x(), pos.y(),
5.0, 5.0,
2024-02-16 13:29:19 -08:00
Anchor::NorthWest,
Anchor::Center
2024-02-04 22:41:26 -08:00
)
);
2024-02-05 10:12:15 -08:00
sprite::set_angle("radar.arrow", angle - 90.0);
sprite::set_color(
2024-02-04 22:41:26 -08:00
"radar.arrow",
Color(
1.0, 1.0, 1.0,
clamp((m - 200.0) /400.0, 0.0, 1.0)
)
)
}
// Ships
{
for s in state.ships() {
let uid = s.phys_uid();
2024-02-04 22:41:26 -08:00
let sprite_name = `radar.ship.${uid}`;
if (
!s.is_some() ||
s.is_dead() ||
s.is_landed()
){
2024-02-05 10:12:15 -08:00
if sprite::exists(sprite_name) {
sprite::remove(sprite_name);
2024-02-04 22:41:26 -08:00
}
continue;
}
let color = Color(0.2, 0.2, 0.2, 1.0);
if s.is_flying() {
color = s.get_faction_color()
}
let size_init = s.get_size() * (1.0 / 50.0);
let pos = s.get_pos();
let d = (pos - p_pos) / radar_range;
let m = d.norm() + (size_init / (2.0 * radar_size));
if m < hide_range {
let size = (hide_range - m) * size_init * shrink_distance;
if size > size_init {
size = size_init;
}
if size < 1.0 {
2024-02-05 10:12:15 -08:00
if sprite::exists(sprite_name) {
sprite::remove(sprite_name);
2024-02-04 22:41:26 -08:00
}
continue;
}
let pos = Vector(radar_size / 2.0 + 5.0, radar_size / -2.0 - 5.0);
pos = pos + (d * radar_size / 2.0);
2024-02-05 10:12:15 -08:00
if !sprite::exists(sprite_name) {
sprite::add(
2024-02-04 22:41:26 -08:00
sprite_name,
"ui::shipblip",
Rect(
pos.x(), pos.y(),
size, size,
2024-02-16 13:29:19 -08:00
Anchor::NorthWest,
Anchor::Center
2024-02-04 22:41:26 -08:00
)
);
} else {
2024-02-05 10:12:15 -08:00
sprite::set_rect(
2024-02-04 22:41:26 -08:00
sprite_name,
Rect(
pos.x(), pos.y(),
size, size,
2024-02-16 13:29:19 -08:00
Anchor::NorthWest,
Anchor::Center
2024-02-04 22:41:26 -08:00
)
);
}
2024-02-05 10:12:15 -08:00
sprite::set_color(sprite_name, color);
2024-02-04 22:41:26 -08:00
}
}
}
// System objects
{
for o in state.objects() {
let l = o.get_index();
let sprite_name = `radar.object.${l}`;
2024-02-04 22:41:26 -08:00
if !o.is_some() {
2024-02-05 10:12:15 -08:00
if sprite::exists(sprite_name) {
sprite::remove(sprite_name);
2024-02-04 22:41:26 -08:00
}
continue;
}
let size_init = (o.get_size() / o.get_pos_z()) / (radar_range * (1.0 / 300.0));
let pos = o.get_pos();
let d = (pos - p_pos) / radar_range;
let m = d.norm() + (size_init / (2.0 * radar_size));
if m < hide_range {
let size = (hide_range - m) * size_init * shrink_distance;
if size > size_init {
size = size_init;
}
if size < 1.0 {
// Don't draw tiny sprites, they flicker
2024-02-05 10:12:15 -08:00
if sprite::exists(sprite_name) {
sprite::remove(sprite_name);
2024-02-04 22:41:26 -08:00
}
continue;
}
let pos = (
Vector(radar_size / 2.0 + 5.0, radar_size / -2.0 - 5.0)
+ (d * radar_size / 2.0)
);
2024-02-05 10:12:15 -08:00
if !sprite::exists(sprite_name) {
sprite::add(
2024-02-04 22:41:26 -08:00
sprite_name,
"ui::planetblip",
Rect(
pos.x(), pos.y(),
size, size,
2024-02-16 13:29:19 -08:00
Anchor::NorthWest,
Anchor::Center
2024-02-04 22:41:26 -08:00
)
);
} else {
2024-02-05 10:12:15 -08:00
sprite::set_rect(
2024-02-04 22:41:26 -08:00
sprite_name,
Rect(
pos.x(), pos.y(),
size, size,
2024-02-16 13:29:19 -08:00
Anchor::NorthWest,
Anchor::Center
2024-02-04 22:41:26 -08:00
)
);
}
}
}
}
// Window frame
{
2024-02-08 20:41:04 -08:00
let dx = (((ui::get_camera_zoom() / 2.0) * state.window_aspect()) / radar_range) * (radar_size / 2.0);
let dy = ((ui::get_camera_zoom() / 2.0) / radar_range) * (radar_size / 2.0);
2024-02-05 10:12:15 -08:00
sprite::set_rect("radar.frame.ne",
2024-02-04 22:41:26 -08:00
Rect(
(radar_size / 2.0 + 5) - dx,
(radar_size / -2.0 - 5) + dy,
3.5, 3.5,
2024-02-16 13:29:19 -08:00
Anchor::NorthWest,
Anchor::Center
2024-02-04 22:41:26 -08:00
)
);
2024-02-05 10:12:15 -08:00
sprite::set_rect("radar.frame.se",
2024-02-04 22:41:26 -08:00
Rect(
(radar_size / 2.0 + 5) - dx,
(radar_size / -2.0 - 5) - dy,
3.5, 3.5,
2024-02-16 13:29:19 -08:00
Anchor::NorthWest,
Anchor::Center
2024-02-04 22:41:26 -08:00
)
);
2024-02-05 10:12:15 -08:00
sprite::set_rect("radar.frame.sw",
2024-02-04 22:41:26 -08:00
Rect(
(radar_size / 2.0 + 5) + dx,
(radar_size / -2.0 - 5) - dy,
3.5, 3.5,
2024-02-16 13:29:19 -08:00
Anchor::NorthWest,
Anchor::Center
2024-02-04 22:41:26 -08:00
)
);
2024-02-05 10:12:15 -08:00
sprite::set_rect("radar.frame.nw",
2024-02-04 22:41:26 -08:00
Rect(
(radar_size / 2.0 + 5) + dx,
(radar_size / -2.0 - 5) + dy,
3.5, 3.5,
2024-02-16 13:29:19 -08:00
Anchor::NorthWest,
Anchor::Center
2024-02-04 22:41:26 -08:00
)
);
2024-02-07 15:58:48 -08:00
}
2024-02-03 11:24:17 -08:00
}