Skip to content

Commit

Permalink
画面の振動演出を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
uTen2c committed Oct 3, 2023
1 parent 59073b4 commit ebd4316
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.uten2c.raincoat.mixin.render;

import dev.uten2c.raincoat.States;
import dev.uten2c.raincoat.shake.ShakeEffectUtils;
import dev.uten2c.raincoat.util.GunState;
import dev.uten2c.raincoat.util.StackUtils;
import net.minecraft.client.MinecraftClient;
Expand All @@ -16,7 +17,9 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(GameRenderer.class)
public abstract class MixinGameRenderer {
Expand All @@ -33,6 +36,16 @@ public abstract class MixinGameRenderer {
@Final
MinecraftClient client;

@Inject(method = "renderWorld", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/GameRenderer;tiltViewWhenHurt(Lnet/minecraft/client/util/math/MatrixStack;F)V", shift = At.Shift.AFTER))
private void shakeEffect(float tickDelta, long limitTime, MatrixStack matrices, CallbackInfo ci) {
final var shakeEffect = States.getShakeEffect();
if (shakeEffect == null || !shakeEffect.shouldPlay()) {
return;
}
final var strength = 1f - (float) Math.pow(shakeEffect.getProgress(), 3.0);
ShakeEffectUtils.shake(matrices, MathHelper.clamp(strength, 0f, 1f) * shakeEffect.getStrength());
}

@Redirect(method = "renderWorld", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/GameRenderer;bobView(Lnet/minecraft/client/util/math/MatrixStack;F)V"))
private void bobView1(GameRenderer instance, MatrixStack matrices, float tickDelta) {
var cameraEntity = client.getCameraEntity();
Expand Down
5 changes: 5 additions & 0 deletions raincoat-fabric/src/main/kotlin/dev/uten2c/raincoat/States.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.uten2c.raincoat

import dev.uten2c.raincoat.debug.DebugShapes
import dev.uten2c.raincoat.shake.ShakeEffect

object States {
@JvmStatic
Expand All @@ -18,11 +19,15 @@ object States {
@JvmStatic
var showDebugShape = false

@JvmStatic
var shakeEffect: ShakeEffect? = null

fun reset() {
isOnServer = false
isHandshakeReceived = false
directionSendRequestedTime = null
isRecoiling = false
shakeEffect = null
DebugShapes.clearShapes()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import dev.uten2c.raincoat.States
import dev.uten2c.raincoat.debug.DebugShape
import dev.uten2c.raincoat.debug.DebugShapes
import dev.uten2c.raincoat.option.OptionManager
import dev.uten2c.raincoat.shake.ShakeEffect
import dev.uten2c.raincoat.util.PacketId
import dev.uten2c.raincoat.util.StackUtils
import kotlinx.coroutines.*
import kotlinx.datetime.Clock
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs
Expand All @@ -24,6 +26,8 @@ import net.minecraft.util.math.Vec3d
import org.slf4j.LoggerFactory
import java.net.URI
import java.util.function.Consumer
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

object Networking {
private val LOGGER = LoggerFactory.getLogger("Raincoat Networking")
Expand All @@ -40,6 +44,8 @@ object Networking {
registerReceiver(Protocol.shapeDisplay) { _, _, buf, _ -> onShapeDisplay(buf) }
registerReceiver(Protocol.shapeDiscard) { _, _, buf, _ -> onShapeDiscard(buf) }
registerReceiver(Protocol.shapeClear) { _, _, buf, _ -> onShapeClear(buf) }
registerReceiver(Protocol.shakePlay) { _, _, buf, _ -> onShakePlay(buf) }
registerReceiver(Protocol.shakeStop) { _, _, _, _ -> onShakeStop() }
}

private fun onHandshakeRequest() {
Expand Down Expand Up @@ -140,6 +146,16 @@ object Networking {
}
}

private fun onShakePlay(buf: PacketByteBuf) {
val duration = buf.readNullable(PacketByteBuf::readVarLong)?.milliseconds ?: Duration.INFINITE
val strength = buf.readFloat()
States.shakeEffect = ShakeEffect(Clock.System.now(), duration, strength)
}

private fun onShakeStop() {
States.shakeEffect = null
}

private fun confirmOpenLink(client: MinecraftClient, parentScreen: Screen?, open: Boolean, uri: URI) {
if (open) {
Util.getOperatingSystem().open(uri)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.uten2c.raincoat.shake

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import net.minecraft.util.math.MathHelper
import kotlin.time.Duration

data class ShakeEffect(val startTime: Instant, val duration: Duration, val strength: Float) {
val progress: Float
get() = MathHelper.clamp(((Clock.System.now() - startTime) / duration).toFloat(), 0f, 1f)

@JvmName("shouldPlay")
fun shouldPlay(): Boolean {
return strength > 0 && Clock.System.now() - startTime < duration
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.uten2c.raincoat.shake

import net.minecraft.client.util.math.MatrixStack
import net.minecraft.util.math.RotationAxis
import kotlin.random.Random

object ShakeEffectUtils {
@JvmStatic
fun shake(matrices: MatrixStack, strength: Float) {
val r1 = randomFloat() * strength
val r2 = randomFloat() * strength
matrices.translate(r1.toDouble() / 4, r2.toDouble() / 8, 0.0)
matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(r1))
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(r2 * 2))
}

private fun randomFloat(): Float {
return Random.nextFloat() - 0.5f
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.uten2c.raincoat
import dev.uten2c.raincoat.util.PacketId

object Protocol {
const val PROTOCOL_VERSION = 6
const val PROTOCOL_VERSION = 7

// C2S
val handshakeResponse = id("handshake/response")
Expand All @@ -22,6 +22,8 @@ object Protocol {
val shapeDisplay = id("shape/display")
val shapeDiscard = id("shape/discard")
val shapeClear = id("shape/clear")
val shakePlay = id("shake/play")
val shakeStop = id("shake/stop")

private fun id(id: String): PacketId {
return PacketId(id)
Expand Down

0 comments on commit ebd4316

Please sign in to comment.