Skip to content

Commit

Permalink
feat: 添加魔法能力管理系统,实现魔法实体生成与消耗,优化玩家交互体验。
Browse files Browse the repository at this point in the history
  • Loading branch information
northgreen committed Apr 19, 2024
1 parent ffb2ab9 commit 6d1cf61
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ loom {
}
}
}
assemble.dependsOn runDatagen
assemble.dependsOn runDatagen
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.2 2024-04-17T20:37:27.0952932 Language
// 1.19.2 2024-04-19T10:01:41.7140308 Language
84f246a88b9c3dc4c5d0c98a56c4de5529eabe74 assets\the_origin_of_magic\lang\en_us.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.2 2024-04-17T20:37:27.0967981 Block Loot Tables
// 1.19.2 2024-04-19T10:01:41.7110246 Block Loot Tables
fe57f13449a11436bbede8c37e01bd0d1cd87191 data\the_origin_of_magic\loot_tables\blocks\magic_workbench.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public abstract class StdThrownMagic extends ThrownEntity implements FlyingItemE
*/
private float exolisionRate;

/**
* 魔力扣除倍率
*/

private float magicRate = 5;

public float getMagicRate() {
return magicRate;
}

/**
* 獲取附加魔法計數
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.ictye.the_origin_of_magic.foundation.Entitys.Magics.Limiters.StdMagicLimiter;
import com.ictye.the_origin_of_magic.foundation.Entitys.Magics.StdThrownMagic;
import com.ictye.the_origin_of_magic.foundation.Items.Magic.StdMagicItem;
import com.ictye.the_origin_of_magic.foundation.player.MagicAbilitiesManager;
import com.ictye.the_origin_of_magic.utils.MagicInventory;
import com.ictye.the_origin_of_magic.utils.PlayerEntityMixinInterfaces;
import net.minecraft.block.BlockState;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.EquipmentSlot;
Expand Down Expand Up @@ -292,7 +294,9 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
float finalScattering = getScattering(); // 計算最終散射

MagicEntity.setVelocity(user, user.getPitch(), user.getYaw(), 0.0F, finalSpeed, finalScattering); // 設置參數
if(world.spawnEntity(MagicEntity)){
MagicAbilitiesManager magicAbilitiesManager = ((PlayerEntityMixinInterfaces)user).the_origin_of_magic$getMagicAbilitiesManager();

if (magicAbilitiesManager.cast(user, MagicEntity, world)) {
// 生成法術實體并且破壞物品
staffItemStack.damage(2, user, e -> e.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.ictye.the_origin_of_magic.foundation.mixin;


import com.ictye.the_origin_of_magic.foundation.player.MagicAbilitiesManager;
import com.ictye.the_origin_of_magic.utils.PlayerEntityMixinInterfaces;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
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.callback.CallbackInfo;

@Mixin(PlayerEntity.class)
public abstract class PlayerEntityMixin implements PlayerEntityMixinInterfaces {

@Unique
protected MagicAbilitiesManager magicAbilitiesManager = new MagicAbilitiesManager();


public MagicAbilitiesManager the_origin_of_magic$getMagicAbilitiesManager() {
return this.magicAbilitiesManager;
}

@Inject(method = "tick",at = @At("HEAD"))
@SuppressWarnings("ConstantValue")
public void tick(CallbackInfo ci){
if((Object)this instanceof ServerPlayerEntity player){
this.magicAbilitiesManager.update(player);
}
}

@Inject(method = "writeCustomDataToNbt",at = @At("HEAD"))
public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci){
magicAbilitiesManager.writeNbt(nbt);
}

@Inject(method = "readCustomDataFromNbt",at = @At("HEAD"))
public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci){
magicAbilitiesManager.readNbt(nbt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ictye.the_origin_of_magic.foundation.mixin;

import com.ictye.the_origin_of_magic.infrastructure.GUI.MagicLevelHud;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(InGameHud.class)
public abstract class PlayerHudMixin extends DrawableHelper{



@Shadow @Final private MinecraftClient client;

@Shadow protected abstract PlayerEntity getCameraPlayer();

@Shadow private int ticks;

@Inject(method = "renderStatusBars", at = @At(value = "HEAD"))
private void renderStatusBars(MatrixStack matrices, CallbackInfo ci){
MagicLevelHud.renderThirstHud(matrices, client, this.getCameraPlayer(), ticks);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ictye.the_origin_of_magic.foundation.mixin;

public class mixin {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.ictye.the_origin_of_magic.foundation.player;

import com.ictye.the_origin_of_magic.foundation.Entitys.Magics.StdThrownMagic;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.world.GameRules;
import net.minecraft.world.World;

public class MagicAbilitiesManager {
private float magicLevel = 20;

private int magicRate = 1;

private int magicTickTimmer = 0;

public float getMagicLevel() {
return magicLevel;
}

/**
* 生成魔法實體
* @param player 玩家
* @param magic 魔法
* @param world 世界
* @return 是否成功
*/
public boolean cast(PlayerEntity player, StdThrownMagic magic , World world){
float neededMagic = magic.getMagicRate() * magicRate;
if(player.isCreative()){
return world.spawnEntity(magic);
}
if(magicLevel>neededMagic){
magicLevel -= neededMagic;
return world.spawnEntity(magic);
}else {
return false;
}
}

public void update(PlayerEntity player){
magicTickTimmer++;
if (!player.getHungerManager().isNotFull() && player.world.getGameRules().getBoolean(GameRules.NATURAL_REGENERATION) && magicLevel < 20&& magicTickTimmer > 10){
magicLevel += 0.5;
magicTickTimmer = 0;
}
}

public void writeNbt(NbtCompound nbt){
nbt.putInt("magicRate", magicRate);
nbt.putInt("magicTickTimmer", magicTickTimmer);
nbt.putFloat("magicLevel", magicLevel);
}

public void readNbt(NbtCompound nbt){
if(nbt.contains("magicLevel", NbtElement.FLOAT_TYPE)){
magicRate = nbt.getInt("magicRate");
magicTickTimmer = nbt.getInt("magicTickTimmer");
magicLevel = nbt.getFloat("magicLevel");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.ictye.the_origin_of_magic.infrastructure.GUI;

import com.ictye.the_origin_of_magic.foundation.player.MagicAbilitiesManager;
import com.ictye.the_origin_of_magic.utils.PlayerEntityMixinInterfaces;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;

public class MagicLevelHud {

private static final Identifier magicTexture = new Identifier("the_origin_of_magic", "textures/gui/magic_power_image.png");
public static void renderThirstHud(MatrixStack matrixStack, MinecraftClient client, PlayerEntity playerEntity, int ticks) {
if (playerEntity != null && !playerEntity.isCreative() && !playerEntity.isSpectator()) {
int width = client.getWindow().getScaledWidth() / 2;
int height = client.getWindow().getScaledHeight();
int bounceFactor = 0;

// Defining the texture
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
RenderSystem.setShaderTexture(0, magicTexture);

MagicAbilitiesManager magicAbilitiesManager = ((PlayerEntityMixinInterfaces)playerEntity).the_origin_of_magic$getMagicAbilitiesManager();

float magicLevel = magicAbilitiesManager.getMagicLevel();

// 繪製空圖標
for(int i = 0; i<10; i++){
DrawableHelper.drawTexture(matrixStack
, width + 82 - (i * 9) + i, (height - 49 + bounceFactor) - 1
, 9, 0
, 9, 9
, 27, 9);
}

// 繪製半個魔力

for (int i = 0; i < 20; i++) {
if(magicLevel!=0){
if (((magicLevel + 1) / 2) > i){
DrawableHelper.drawTexture(
matrixStack,
width + 82 - (i * 9) + i , (height - 49) - 1,
18, 0,
9, 9,
27, 9);
}
}
}

for (int i = 0; i < 20; i++) {
if(magicLevel!=0){
if ((magicLevel / 2) > i){
DrawableHelper.drawTexture(
matrixStack,
width + 82 - (i * 9) + i , (height - 49) - 1,
0, 0,
9, 9,
27, 9);
}
}
}

RenderSystem.setShaderTexture(0, DrawableHelper.GUI_ICONS_TEXTURE);
}
}
}
4 changes: 0 additions & 4 deletions src/main/java/com/ictye/the_origin_of_magic/mixin.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ictye.the_origin_of_magic.utils;

import com.ictye.the_origin_of_magic.foundation.player.MagicAbilitiesManager;

public interface PlayerEntityMixinInterfaces {
MagicAbilitiesManager the_origin_of_magic$getMagicAbilitiesManager();
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/main/resources/the_origin_of_magic.mixins.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"required": true,
"minVersion": "0.8",
"package": "com.ictye.the_origin_of_magic.mixin",
"package": "com.ictye.the_origin_of_magic.foundation.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"PlayerEntityMixin"
],
"client": [
"PlayerHudMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 6d1cf61

Please sign in to comment.