Skip to content

Instantly share code, notes, and snippets.

View RJ's full-sized avatar
💤

Richard Jones RJ

💤
View GitHub Profile
@RJ
RJ / bullet_plugin.rs
Last active November 23, 2024 11:14
spacepit bullet plugin, observer vs system ordering issue for PreSpawnedPlayerObject hashes
use crate::prelude::*;
use avian2d::prelude::*;
use bevy::prelude::*;
use lightyear::client::components::ComponentSyncMode;
use serde::{Deserialize, Serialize};
pub mod prelude {
pub const BULLET_SIZE: f32 = 1.5;
pub use super::Bullet;
pub use super::BulletBundle;
@RJ
RJ / player_commands.rs
Created November 21, 2024 13:22
bevy plugin managing mouse inputs etc
use std::ops::Mul;
/// Handles acting on player inputs. Moving and shooting.
use crate::prelude::*;
use avian2d::prelude::*;
use bevy::{ecs::query::QueryData, prelude::*};
use client::{is_in_rollback, Predicted};
use leafwing_input_manager::{Actionlike, InputControlKind};
use lightyear::client::input::leafwing::InputSystemSet;
use lightyear::{
@RJ
RJ / certgen.rs
Created October 14, 2024 08:40
WebTransport compatible self signed certificate generation in rust using openssl bindings
// Ended up not using this because i discovered `wtransport` lib has a SelfSigned builder which does it for me.
use openssl::asn1::Asn1Time;
use openssl::ec::{EcGroup, EcKey};
use openssl::hash::MessageDigest;
use openssl::nid::Nid;
use openssl::pkey::PKey;
use openssl::x509::extension::SubjectAlternativeName;
use openssl::x509::extension::{BasicConstraints, SubjectKeyIdentifier};
@RJ
RJ / bevy fn to return system.rs
Last active September 3, 2024 07:49
return a system from a function, enclosing something useful
/*
Sometimes you want to dump some component values in multiple different schedules
like when debugging network replication..
here's an example of a function you can use to enclose the schedule name you put it in:
```rust
app.add_systems(Update, dumper("Update"));
app.add_systems(Last, dumper("Last"));
```
@RJ
RJ / fixed_joint_2d.rs
Created September 2, 2024 12:29
Modified fixed_joint_2d example with rotation axes note
use avian2d::{math::*, prelude::*};
use bevy::{ecs::component::StorageType, prelude::*};
use examples_common_2d::ExampleCommonPlugin;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
ExampleCommonPlugin,
PhysicsPlugins::default(),
@RJ
RJ / renet_lag.markdown
Last active September 11, 2023 16:27
Testing multiple renet clients with varying simulated latency levels

Testing with varying levels of lag per Renet client

I want to run multiple clients locally, each with different amounts of lag.

Achieved by specifying the client's local port number when binding socket, and setting up a pf rule based on local port. (all connecting to renet's default port of 5000)

This is for Mac OS, but something similar would be possible on linux.

#!/bin/bash -ex
@RJ
RJ / main.rs
Created August 17, 2023 09:03
Example of passing a system set label to a bevy plugin so the plugin can configure sets as needed, using BoxedSystemSet
use bevy::{prelude::*, ecs::schedule::SystemSetConfig};
use bevy::ecs::schedule::BoxedSystemSet;
//// game:
#[derive(SystemSet, Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MySets {
GameLogic,
}
#[derive(Resource)]
@RJ
RJ / Cargo.toml
Last active July 1, 2021 11:48
bevy span lifetime
[package]
name = "bevylog"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = "0.5"
@RJ
RJ / bevy_nested_schedule_stage_adding.rs
Created June 12, 2021 11:48
Example of adding systems to stages nested in deep schedules, with bevy 0.5. Trait to add feature to AppBuilder - add_system_to_stage only works if the stage is in the root schedule.
pub trait AddSpawnSystem {
fn add_spawning_system(&mut self, system: impl Into<SystemDescriptor>) -> &mut AppBuilder;
}
impl AddSpawnSystem for AppBuilder {
fn add_spawning_system(&mut self, system: impl Into<SystemDescriptor>) -> &mut AppBuilder {
self.app.schedule.stage(stage::MAIN_SCHEDULE, |main_schedule: &mut Schedule| {
main_schedule.stage(stage::SIMULATION_SCHEDULE, |sim_schedule: &mut Schedule| {
sim_schedule.add_system_to_stage(Stages::SpawningStage, system)
})
@RJ
RJ / HALJIA USB Relay Module.py
Created May 11, 2018 15:29
Python to control cheap USB relay from amazon: QinHeng Electronics HL-340 USB-Serial adapter
# To run with pyusb debugging:
#
# PYUSB_DEBUG=debug python relay.py
#
# Grab the vendor and product codes from syslog when plugging in the relay:
#
# usb 3-1: New USB device found, idVendor=1a86, idProduct=7523
#
import time
import usb.core