Galactica/content/ui/flying.rhai

301 lines
6.2 KiB
Plaintext

fn init(state) {
conf::show_starfield(true);
conf::show_phys(true);
sprite::add(
"ring",
"ui::status",
Rect(
-5.0, -5.0, 100.0, 100.0,
Anchor::NorthEast,
Anchor::NorthEast
)
);
radialbar::add(
"shield", 2.5,
Color(0.3, 0.6, 0.8, 1.0),
Rect(
-9.5, -9.5, 91.0, 91.0,
Anchor::NorthEast,
Anchor::NorthEast
)
);
radialbar::add(
"hull", 2.5,
Color(0.8, 0.7, 0.5, 1.0),
Rect(
-13.5, -13.5, 83.0, 83.0,
Anchor::NorthEast,
Anchor::NorthEast
)
);
let radar_size = 150.0;
let radar_range = 4000.0;
sprite::add(
"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,
Anchor::Center,
Anchor::NorthWest
);
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));
sprite::add("radar.arrow", "ui::centerarrow", init_pos);
}
fn event(state, event) {
if type_of(event) == "PlayerShipStateEvent" {
if state.player_ship().is_landed() {
ui::go_to_scene("landed");
return;
}
return;
}
}
fn step(state) {
radialbar::set_val("shield",
state.player_ship().get_shields()
/ state.player_ship().get_total_shields()
);
radialbar::set_val("hull",
state.player_ship().get_hull()
/ state.player_ship().get_total_hull()
);
// TODO: share variables with init();
let radar_size = 150.0;
let radar_range = 4000.0;
let hide_range = 0.85;
let shrink_distance = 20.0;
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);
sprite::set_rect("radar.arrow",
Rect(
pos.x(), pos.y(),
5.0, 5.0,
Anchor::Center,
Anchor::NorthWest
)
);
sprite::set_angle("radar.arrow", angle - 90.0);
sprite::set_color(
"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.get_uid();
let sprite_name = `radar.ship.${uid}`;
if (
!s.is_some() ||
s.is_dead() ||
s.is_landed()
){
if sprite::exists(sprite_name) {
sprite::remove(sprite_name);
}
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 {
if sprite::exists(sprite_name) {
sprite::remove(sprite_name);
}
continue;
}
let pos = Vector(radar_size / 2.0 + 5.0, radar_size / -2.0 - 5.0);
pos = pos + (d * radar_size / 2.0);
if !sprite::exists(sprite_name) {
sprite::add(
sprite_name,
"ui::shipblip",
Rect(
pos.x(), pos.y(),
size, size,
Anchor::Center,
Anchor::NorthWest
)
);
} else {
sprite::set_rect(
sprite_name,
Rect(
pos.x(), pos.y(),
size, size,
Anchor::Center,
Anchor::NorthWest
)
);
}
sprite::set_color(sprite_name, color);
}
}
}
// System objects
{
for o in state.objects() {
let sprite_name = `radar.object.${o.get_label()}`;
if !o.is_some() {
if sprite::exists(sprite_name) {
sprite::remove(sprite_name);
}
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
if sprite::exists(sprite_name) {
sprite::remove(sprite_name);
}
continue;
}
let pos = (
Vector(radar_size / 2.0 + 5.0, radar_size / -2.0 - 5.0)
+ (d * radar_size / 2.0)
);
if !sprite::exists(sprite_name) {
sprite::add(
sprite_name,
"ui::planetblip",
Rect(
pos.x(), pos.y(),
size, size,
Anchor::Center,
Anchor::NorthWest
)
);
} else {
sprite::set_rect(
sprite_name,
Rect(
pos.x(), pos.y(),
size, size,
Anchor::Center,
Anchor::NorthWest
)
);
}
}
}
}
// Window frame
{
let dx = (((state.camera_zoom() / 2.0) * state.window_aspect()) / radar_range) * (radar_size / 2.0);
let dy = ((state.camera_zoom() / 2.0) / radar_range) * (radar_size / 2.0);
sprite::set_rect("radar.frame.ne",
Rect(
(radar_size / 2.0 + 5) - dx,
(radar_size / -2.0 - 5) + dy,
3.5, 3.5,
Anchor::Center,
Anchor::NorthWest
)
);
sprite::set_rect("radar.frame.se",
Rect(
(radar_size / 2.0 + 5) - dx,
(radar_size / -2.0 - 5) - dy,
3.5, 3.5,
Anchor::Center,
Anchor::NorthWest
)
);
sprite::set_rect("radar.frame.sw",
Rect(
(radar_size / 2.0 + 5) + dx,
(radar_size / -2.0 - 5) - dy,
3.5, 3.5,
Anchor::Center,
Anchor::NorthWest
)
);
sprite::set_rect("radar.frame.nw",
Rect(
(radar_size / 2.0 + 5) + dx,
(radar_size / -2.0 - 5) + dy,
3.5, 3.5,
Anchor::Center,
Anchor::NorthWest
)
);
}
}