// StarKingdoms.IO, a browser game about drifting through space // Copyright (C) 2023 ghostly_zsh, TerraMaster85, core // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . use std::net::Ipv4Addr; use crate::mathutil::rot2d; use bevy::log::Level; use bevy::log::LogPlugin; use bevy::math::{vec2, vec3}; use bevy::{ecs::event::ManualEventReader, prelude::*}; use bevy_rapier2d::prelude::*; use bevy_twite::{twite::frame::MessageType, ServerEvent, TwiteServerConfig, TwiteServerPlugin}; use component::Input; use component::*; use packet::*; use rand::Rng; use starkingdoms_common::{pack_savefile, unpack_savefile, SaveData}; use std::f32::consts::PI; pub mod component; pub mod macros; pub mod mathutil; pub mod packet; const SCALE: f32 = 10.0; const EARTH_SIZE: f32 = 1000.0; const MOON_SIZE: f32 = EARTH_SIZE / 4.; const MARS_SIZE: f32 = EARTH_SIZE / 2.; const EARTH_MASS: f32 = 10000.0; const MOON_MASS: f32 = EARTH_MASS / 30.; const MARS_MASS: f32 = EARTH_MASS / 8.; const GRAVITY: f32 = 0.02; const PART_HALF_SIZE: f32 = 25.0; const HEARTY_THRUSTER_FORCE: f32 = 0.08; const LANDING_THRUSTER_FORCE: f32 = 5.; // maybe make this only cargo modules later const FREE_MODULE_CAP: usize = 30; fn main() { // read the key in let key = std::fs::read_to_string("/etc/starkingdoms/app_key").unwrap(); App::new() .insert_resource(AppKeys { app_key: key }) .insert_resource(TwiteServerConfig { addr: Ipv4Addr::new(0, 0, 0, 0), port: 3000, }) .add_plugins(MinimalPlugins) .add_plugins(LogPlugin { level: Level::DEBUG, filter: "wgpu=error,bevy_render=info,bevy_ecs=trace".to_string(), }) .insert_resource(RapierConfiguration { gravity: Vect { x: 0.0, y: 0.0 }, ..Default::default() }) .init_resource::() .add_plugins(RapierPhysicsPlugin::::pixels_per_meter(SCALE)) .add_plugins(TwiteServerPlugin) .add_systems(Startup, setup_integration_parameters) .add_systems(Startup, spawn_planets) .add_systems(FixedUpdate, module_spawn) .add_systems(Update, on_message) .add_systems(Update, on_close) .add_systems(FixedUpdate, on_position_change) .add_systems( FixedUpdate, (break_modules, gravity_update, player_input_update).chain(), ) .add_systems(FixedUpdate, convert_modules) //.insert_resource(Time::::from_seconds(1.0/20.0)) .run(); info!("Goodbye!"); } fn setup_integration_parameters(mut context: ResMut) { context.integration_parameters.dt = 1.0 / 60.0; context.integration_parameters.joint_erp = 0.2; context.integration_parameters.erp = 0.5; context.integration_parameters.max_stabilization_iterations = 16; } fn spawn_planets(mut commands: Commands) { info!("Spawning planets"); let earth_pos = Transform::from_xyz(0.0, 0.0, 0.0); commands .spawn(PlanetBundle { planet_type: PlanetType::Earth, transform: TransformBundle::from(earth_pos), }) .insert(Collider::ball(EARTH_SIZE / SCALE)) .insert(AdditionalMassProperties::Mass(EARTH_MASS)) .insert(ReadMassProperties::default()) .with_children(|children| { children .spawn(Collider::ball((EARTH_SIZE + 3.) / SCALE)) .insert(ActiveEvents::COLLISION_EVENTS) .insert(Sensor); }) .insert(RigidBody::Fixed); let moon_pos = Transform::from_xyz(3000.0 / SCALE, 0.0, 0.0); commands .spawn(PlanetBundle { planet_type: PlanetType::Moon, transform: TransformBundle::from(moon_pos), }) .insert(Collider::ball(MOON_SIZE / SCALE)) .insert(AdditionalMassProperties::Mass(MOON_MASS)) .insert(ReadMassProperties::default()) .with_children(|children| { children .spawn(Collider::ball((MOON_SIZE + 3.) / SCALE)) .insert(ActiveEvents::COLLISION_EVENTS) .insert(Sensor); }) .insert(RigidBody::Fixed); let mars_pos = Transform::from_xyz(3000.0 / SCALE, 700.0 / SCALE, 0.0); commands .spawn(PlanetBundle { planet_type: PlanetType::Mars, transform: TransformBundle::from(mars_pos), }) .insert(Collider::ball(MARS_SIZE / SCALE)) .insert(AdditionalMassProperties::Mass(MARS_MASS)) .insert(ReadMassProperties::default()) .with_children(|children| { children .spawn(Collider::ball((MARS_SIZE + 3.) / SCALE)) .insert(ActiveEvents::COLLISION_EVENTS) .insert(Sensor); }) .insert(RigidBody::Fixed); } fn module_spawn( mut commands: Commands, time: Res