Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Juicing machine gun #238

Merged
merged 12 commits into from
Nov 12, 2021
59 changes: 38 additions & 21 deletions assets/items/machine_gun.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,56 @@
},
"type": "weapon",
"cooldown": 0.15,
"attack_duration": 0.1,
"recoil": 125.0,
"uses": 24,
"mount_offset": {
"x": -16,
"y": -4
},
"effect_offset": {
"x": 56,
"x": 64,
"y": 4
},
"sound_effect": "shoot",
"effects": {
"type": "projectile",
"range": 128.0,
"speed": 12.0,
"spread": 5.0,
"projectile": {
"type": "sprite",
"sprite": {
"texture": "small_projectile",
"size": {
"x": 8,
"y": 4
"effects": [
{
"type": "projectile",
"range": 512.0,
"speed": 8.0,
"spread": 5.0,
"projectile": {
"type": "sprite",
"sprite": {
"texture": "machine_gun_bullet",
"size": {
"x": 30,
"y": 4
},
"offset": {
"x": -15,
"y": -2
}
},
"tint": {
"r": 0.9,
"g": 0.75,
"b": 0.12,
"a": 1.0
}
"is_rotated": true
}
},
{
"type": "particle_controller",
"id": "0",
"particle_id": "machine_gun_muzzle_smoke",
"start_delay": 0.2,
"is_can_be_interrupted": true,
"amount": 5,
"interval": 0.05
},
{
"type": "particle_controller",
"id": "1",
"particle_id": "machine_gun_firing_smoke",
"amount": 1
}
},
],
"collider_size": {
"x": 68,
"y": 24
Expand All @@ -55,7 +72,7 @@
{
"id": "attack",
"row": 1,
"frames": 2,
"frames": 1,
"fps": 8,
"is_looping": true
}
Expand Down
8 changes: 8 additions & 0 deletions assets/particle_effects.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,13 @@
{
"id": "smoke",
"path": "particle_effects/smoke.json"
},
{
"id": "machine_gun_muzzle_smoke",
"path": "particle_effects/machine_gun_muzzle_smoke.json"
},
{
"id": "machine_gun_firing_smoke",
"path": "particle_effects/machine_gun_firing_smoke.json"
}
]
73 changes: 73 additions & 0 deletions assets/particle_effects/machine_gun_firing_smoke.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"local_coords": false,
"emission_shape": {
"Sphere": {
"radius": 3
}
},
"one_shot": true,
"lifetime": 0.29999995,
"lifetime_randomness": 0,
"explosiveness": 1,
"amount": 1,
"shape": {
"Circle": {
"subdivisions": 16
}
},
"emitting": false,
"initial_direction": {
"x": 0,
"y": -1
},
"initial_direction_spread": 0.3,
"initial_velocity": 54.300003,
"initial_velocity_randomness": 0.4,
"linear_accel": -0.89999986,
"size": 2,
"size_randomness": 0,
"size_curve": {
"points": [
[
0.005,
1.36
],
[
0.49,
1.84
],
[
1,
0
]
],
"interpolation": "Linear",
"resolution": 30
},
"blend_mode": "Alpha",
"colors_curve": {
"start": {
"r": 0.6818182,
"g": 0.6818182,
"b": 0.6818182,
"a": 1
},
"mid": {
"r": 0.77272725,
"g": 0.77272725,
"b": 0.77272725,
"a": 1
},
"end": {
"r": 1,
"g": 1,
"b": 1,
"a": 1
}
},
"gravity": {
"x": 0,
"y": -54.1
},
"post_processing": {}
}
73 changes: 73 additions & 0 deletions assets/particle_effects/machine_gun_muzzle_smoke.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"local_coords": false,
"emission_shape": {
"Sphere": {
"radius": 3
}
},
"one_shot": true,
"lifetime": 0.7999999,
"lifetime_randomness": 0,
"explosiveness": 1,
"amount": 1,
"shape": {
"Circle": {
"subdivisions": 16
}
},
"emitting": false,
"initial_direction": {
"x": 0,
"y": -1
},
"initial_direction_spread": 0.3,
"initial_velocity": 54.300003,
"initial_velocity_randomness": 0.4,
"linear_accel": -0.89999986,
"size": 2.9,
"size_randomness": 0,
"size_curve": {
"points": [
[
0,
0.88
],
[
0.49,
1.88
],
[
1,
0
]
],
"interpolation": "Linear",
"resolution": 30
},
"blend_mode": "Alpha",
"colors_curve": {
"start": {
"r": 0.29545453,
"g": 0.29545453,
"b": 0.29545453,
"a": 1
},
"mid": {
"r": 0.52272725,
"g": 0.52272725,
"b": 0.52272725,
"a": 1
},
"end": {
"r": 0.84090906,
"g": 0.84090906,
"b": 0.84090906,
"a": 1
}
},
"gravity": {
"x": 0,
"y": -54.1
},
"post_processing": {}
}
4 changes: 4 additions & 0 deletions assets/textures.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,5 +314,9 @@
{
"id": "musket_bullet",
"path": "textures/items/MusketBullet(15x15).png"
},
{
"id": "machine_gun_bullet",
"path": "textures/items/MachineGunBullet(7x4).png"
}
]
Binary file added assets/textures/items/MachineGunBullet(7x4).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 9 additions & 5 deletions src/components/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ pub struct SpriteParams {
/// The pivot of the sprite, relative to the position provided as an argument to the `Sprite`
/// draw method, plus any offset.
/// Note that this offset will not be inverted if the sprite is flipped.
#[serde(default, with = "json::vec2_def")]
pub pivot: Vec2,
#[serde(
default,
with = "json::vec2_opt",
skip_serializing_if = "Option::is_none"
)]
pub pivot: Option<Vec2>,
/// The size of the drawn sprite. If no size is specified, the texture entry's `sprite_size`
/// will be used, if specified, or the raw texture size, if not.
#[serde(
Expand All @@ -49,7 +53,7 @@ impl Default for SpriteParams {
texture_id: "".to_string(),
index: 0,
offset: Vec2::ZERO,
pivot: Vec2::ZERO,
pivot: None,
size: None,
tint: None,
is_deactivated: false,
Expand All @@ -63,7 +67,7 @@ pub struct Sprite {
source_rect: Rect,
tint: Color,
offset: Vec2,
pivot: Vec2,
pivot: Option<Vec2>,
pub is_deactivated: bool,
}

Expand Down Expand Up @@ -139,7 +143,7 @@ impl Sprite {
rotation,
source: Some(self.source_rect),
dest_size: Some(size),
pivot: Some(self.pivot),
pivot: self.pivot,
},
)
}
Expand Down
44 changes: 38 additions & 6 deletions src/effects/active/projectiles.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::f32::consts::PI;

use macroquad::{
experimental::{
collections::storage,
Expand Down Expand Up @@ -33,6 +35,10 @@ pub enum ProjectileKind {
Sprite {
#[serde(rename = "sprite")]
params: Option<SpriteParams>,
#[serde(default)]
/// If yes, the sprite would be rotated by angle between Vec2(1, 0) (most likely will be changed in the future) and velocity vector.
/// This, for example, used for machine gun bullets rotation.
is_rotated: bool,
},
}

Expand All @@ -45,6 +51,7 @@ struct Projectile {
velocity: Vec2,
range: f32,
sprite: Option<Sprite>,
sprite_draw_angle: f32,
particle_controller: Option<ParticleController>,
}

Expand All @@ -67,12 +74,31 @@ impl Projectiles {
range: f32,
particle_params: Option<ParticleControllerParams>,
) {
let sprite = if let ProjectileKind::Sprite { params } = &mut kind {
let mut sprite = None;

let mut sprite_draw_angle = 0.0;

if let ProjectileKind::Sprite { params, is_rotated } = &mut kind {
let params = params.take().unwrap();
Some(Sprite::new(params))
} else {
None
};
sprite = Some(Sprite::new(params));

if *is_rotated {
let mut vec = Vec2::new(0.0, 0.0);

if velocity.x < 0.0 {
vec.x = 1.0;
} else {
vec.x = -1.0;
}

sprite_draw_angle = (velocity.y - vec.y).atan2(velocity.x - vec.x);

// By this, we have correct sprite rotation after flipping sprite by X
if velocity.x < 0.0 {
sprite_draw_angle += PI;
}
}
}

let particle_controller = particle_params.map(ParticleController::new);

Expand All @@ -84,6 +110,7 @@ impl Projectiles {
velocity,
range,
sprite,
sprite_draw_angle,
particle_controller,
});
}
Expand Down Expand Up @@ -192,7 +219,12 @@ impl Node for Projectiles {
ProjectileKind::Sprite { .. } => {
let sprite = projectile.sprite.as_ref().unwrap();
let flip_x = projectile.velocity.x < 0.0;
sprite.draw(projectile.position, 0.0, flip_x, false);
sprite.draw(
projectile.position,
projectile.sprite_draw_angle,
flip_x,
false,
);
}
}
}
Expand Down