Skip to content

Commit

Permalink
现在输赢有动画了
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Oct 31, 2024
1 parent 058b0f3 commit 939cb58
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public static void maidMove(ServerPlayer player, Level level, BlockPos pos, int
&& sit.getFirstPassenger() instanceof EntityMaid maid && maid.isOwnedBy(player)) {
// TODO: 暂时不加段位系统
maid.getFavorabilityManager().apply(Type.CCHESS_WIN);
maid.getGameRecordManager().markStatue(true);
InitTrigger.MAID_EVENT.trigger(player, TriggerType.WIN_CCHESS);
}

Expand Down Expand Up @@ -122,6 +123,9 @@ public static void maidMove(ServerPlayer player, Level level, BlockPos pos, int

if (level instanceof ServerLevel serverLevel && serverLevel.getEntity(sitId) instanceof EntitySit sit && sit.getFirstPassenger() instanceof EntityMaid maid) {
maid.swing(InteractionHand.MAIN_HAND);
if (playerLost) {
maid.getGameRecordManager().markStatue(false);
}
}
level.playSound(null, pos, InitSounds.GOMOKU.get(), SoundSource.BLOCKS, 1.0f, 0.8F + level.random.nextFloat() * 0.4F);
chess.refresh();
Expand Down Expand Up @@ -228,7 +232,7 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
if (clickResetArea) {
chess.reset();
chess.refresh();
level.playSound(null, centerPos, InitSounds.GOMOKU_RESET.get(), SoundSource.BLOCKS, 1.0f, 1.0f);
maid.getGameRecordManager().resetStatue();
}

// 没有点击到棋盘上,返回
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
level.playSound(null, centerPos, InitSounds.GOMOKU_RESET.get(), SoundSource.BLOCKS, 1.0f, 1.0f);
gomoku.reset();
gomoku.refresh();

// 重置女仆棋类动画
Entity sitEntity = serverLevel.getEntity(gomoku.getSitId());
if (sitEntity != null && sitEntity.isAlive() && sitEntity.getFirstPassenger() instanceof EntityMaid maid) {
maid.getGameRecordManager().resetStatue();
}

return InteractionResult.SUCCESS;
}
Entity sitEntity = serverLevel.getEntity(gomoku.getSitId());
Expand Down Expand Up @@ -268,8 +275,9 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
// 但是和其他人的女仆对弈不加好感哦
if (statue == Statue.WIN && maid.isOwnedBy(player)) {
maid.getFavorabilityManager().apply(Type.GOMOKU_WIN);
maid.getGameRecordManager().markStatue(true);
int rankBefore = MaidGomokuAI.getRank(maid);
MaidGomokuAI.addMaidCount(maid);
maid.getGameRecordManager().increaseGomokuWinCount();
int rankAfter = MaidGomokuAI.getRank(maid);
// 女仆升段啦
if (rankBefore < rankAfter) {
Expand All @@ -283,7 +291,7 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
level.playSound(null, pos, InitSounds.GOMOKU.get(), SoundSource.BLOCKS, 1.0f, 0.8F + level.random.nextFloat() * 0.4F);
if (gomoku.isInProgress()) {
gomoku.setPlayerTurn(false);
NetworkHandler.sendToClientPlayer(new GomokuToClientMessage(centerPos, chessData, playerPoint, MaidGomokuAI.getMaidCount(maid)), player);
NetworkHandler.sendToClientPlayer(new GomokuToClientMessage(centerPos, chessData, playerPoint, maid.getGameRecordManager().getGomokuWinCount()), player);
}
gomoku.refresh();
return InteractionResult.SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static void maidMove(ServerPlayer player, Level level, BlockPos pos, int
&& sit.getFirstPassenger() instanceof EntityMaid maid && maid.isOwnedBy(player)) {
// TODO: 暂时不加段位系统
maid.getFavorabilityManager().apply(Type.WCHESS_WIN);
maid.getGameRecordManager().markStatue(true);
InitTrigger.MAID_EVENT.trigger(player, TriggerType.WIN_WCHESS);
}

Expand Down Expand Up @@ -125,6 +126,9 @@ public static void maidMove(ServerPlayer player, Level level, BlockPos pos, int

if (level instanceof ServerLevel serverLevel && serverLevel.getEntity(sitId) instanceof EntitySit sit && sit.getFirstPassenger() instanceof EntityMaid maid) {
maid.swing(InteractionHand.MAIN_HAND);
if (playerLost) {
maid.getGameRecordManager().markStatue(false);
}
}
level.playSound(null, pos, InitSounds.GOMOKU.get(), SoundSource.BLOCKS, 1.0f, 0.8F + level.random.nextFloat() * 0.4F);
chess.refresh();
Expand Down Expand Up @@ -232,6 +236,7 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
chess.reset();
chess.refresh();
level.playSound(null, centerPos, InitSounds.GOMOKU_RESET.get(), SoundSource.BLOCKS, 1.0f, 1.0f);
maid.getGameRecordManager().resetStatue();
}

// 没有点击到棋盘上,返回
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.github.tartaricacid.touhoulittlemaid.client.entity.GeckoMaidEntity;
import com.github.tartaricacid.touhoulittlemaid.compat.tacz.TacCompat;
import com.github.tartaricacid.touhoulittlemaid.entity.item.EntityChair;
import com.github.tartaricacid.touhoulittlemaid.entity.item.EntitySit;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.MaidGameRecordManager;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.core.PlayState;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.core.builder.AnimationBuilder;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.core.builder.ILoopType;
Expand Down Expand Up @@ -233,11 +236,22 @@ public PlayState predicateUse(AnimationEvent<GeckoMaidEntity<?>> event) {
return PlayState.STOP;
}

public PlayState predicateBeg(AnimationEvent<GeckoMaidEntity<?>> event) {
public PlayState predicateMisc(AnimationEvent<GeckoMaidEntity<?>> event) {
IMaid maid = event.getAnimatableEntity().getMaid();
if (maid == null) {
return PlayState.STOP;
}
// 赢棋输棋优先
if (maid instanceof EntityMaid entityMaid && entityMaid.getVehicle() instanceof EntitySit) {
MaidGameRecordManager manager = entityMaid.getGameRecordManager();
if (manager.isWin()) {
return playAnimation(event, "game_win", ILoopType.EDefaultLoopTypes.LOOP);
}
if (manager.isLost()) {
return playAnimation(event, "game_lost", ILoopType.EDefaultLoopTypes.LOOP);
}
}
// 祈求动画
if (maid.isBegging()) {
return playAnimation(event, "beg", ILoopType.EDefaultLoopTypes.LOOP);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.tartaricacid.touhoulittlemaid.TouhouLittleMaid;
import com.github.tartaricacid.touhoulittlemaid.api.entity.IMaid;
import com.github.tartaricacid.touhoulittlemaid.client.animation.gecko.AnimationManager;
import com.github.tartaricacid.touhoulittlemaid.client.animation.gecko.AnimationRegister;
import com.github.tartaricacid.touhoulittlemaid.client.resource.pojo.MaidModelInfo;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.core.AnimatableEntity;
import com.github.tartaricacid.touhoulittlemaid.geckolib3.core.controller.AnimationController;
Expand Down Expand Up @@ -55,7 +54,7 @@ public void registerControllers() {
addAnimationController(new AnimationController<>(this, "hold_mainhand", 0, manager::predicateMainhandHold));
addAnimationController(new AnimationController<>(this, "swing", 2, manager::predicateSwing));
addAnimationController(new AnimationController<>(this, "use", 2, manager::predicateUse));
addAnimationController(new AnimationController<>(this, "beg", 2, manager::predicateBeg));
addAnimationController(new AnimationController<>(this, "misc", 2, manager::predicateMisc));
addAnimationController(new AnimationController<>(this, "passenger", 2, manager::predicatePassengerAnimation));
for (int i = 0; i < 8; i++) {
String controllerName = String.format("parallel_%d_controller", i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ private void renderMaidInfo(GuiGraphics graphics, int mouseX, int mouseY) {
list.add(Component.literal(prefix).withStyle(ChatFormatting.WHITE)
.append(Component.translatable("block.touhou_little_maid.gomoku")
.append(":\u0020").withStyle(ChatFormatting.AQUA))
.append(Component.translatable("tooltips.touhou_little_maid.info.game_skill.gomoku", MaidGomokuAI.getMaidCount(maid), MaidGomokuAI.getRank(maid))));
.append(Component.translatable("tooltips.touhou_little_maid.info.game_skill.gomoku", maid.getGameRecordManager().getGomokuWinCount(), MaidGomokuAI.getRank(maid))));

graphics.renderComponentTooltip(font, list, mouseX, mouseY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.github.tartaricacid.touhoulittlemaid.api.game.gomoku.Statue;
import com.github.tartaricacid.touhoulittlemaid.api.game.gomoku.ZhiZhangAIService;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;

public final class MaidGomokuAI {
public static final AIService EASY = new ZhiZhangAIService(new AIService.AIConfig(1, 10, false, 0, 6));
Expand All @@ -18,7 +16,7 @@ public final class MaidGomokuAI {
public static final int HARD_COUNT = 24;

public static int getRank(EntityMaid maid) {
int maidCount = getMaidCount(maid);
int maidCount = maid.getGameRecordManager().getGomokuWinCount();
if (maidCount <= EASY_COUNT) {
return 1;
} else if (maidCount <= NORMAL_COUNT) {
Expand All @@ -30,36 +28,18 @@ public static int getRank(EntityMaid maid) {
}
}

public static AIService getService(int maidCount) {
if (maidCount <= EASY_COUNT) {
public static AIService getService(int winCount) {
if (winCount <= EASY_COUNT) {
return EASY;
} else if (maidCount <= NORMAL_COUNT) {
} else if (winCount <= NORMAL_COUNT) {
return NORMAL;
} else if (maidCount <= HARD_COUNT) {
} else if (winCount <= HARD_COUNT) {
return HARD;
} else {
return HELL;
}
}

public static int getMaidCount(EntityMaid maid) {
CompoundTag gameSkill = maid.getGameSkill();
if (gameSkill.contains("Gomoku", Tag.TAG_INT)) {
return gameSkill.getInt("Gomoku");
}
return 0;
}

public static void addMaidCount(EntityMaid maid) {
CompoundTag gameSkill = maid.getGameSkill();
if (gameSkill.contains("Gomoku", Tag.TAG_INT)) {
gameSkill.putInt("Gomoku", gameSkill.getInt("Gomoku") + 1);
} else {
gameSkill.putInt("Gomoku", 1);
}
maid.setGameSkill(gameSkill);
}

/**
* 获取棋局状态
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ public class EntityMaid extends TamableAnimal implements CrossbowAttackMob, IMai
private static final EntityDataAccessor<String> BACKPACK_TYPE = SynchedEntityData.defineId(EntityMaid.class, EntityDataSerializers.STRING);
private static final EntityDataAccessor<ItemStack> BACKPACK_ITEM_SHOW = SynchedEntityData.defineId(EntityMaid.class, EntityDataSerializers.ITEM_STACK);
private static final EntityDataAccessor<String> BACKPACK_FLUID = SynchedEntityData.defineId(EntityMaid.class, EntityDataSerializers.STRING);
private static final EntityDataAccessor<CompoundTag> GAME_SKILL = SynchedEntityData.defineId(EntityMaid.class, EntityDataSerializers.COMPOUND_TAG);

// 游戏数据记录,包括赢棋次数和赢棋状态
static final EntityDataAccessor<CompoundTag> GAME_SKILL = SynchedEntityData.defineId(EntityMaid.class, EntityDataSerializers.COMPOUND_TAG);
static final EntityDataAccessor<Byte> GAME_STATUE = SynchedEntityData.defineId(EntityMaid.class, EntityDataSerializers.BYTE);

// 给 MaidConfigManager 用的,必须在这里声明,避免 ID 不同步
static final EntityDataAccessor<Boolean> DATA_PICKUP = SynchedEntityData.defineId(EntityMaid.class, EntityDataSerializers.BOOLEAN);
Expand All @@ -191,7 +194,6 @@ public class EntityMaid extends TamableAnimal implements CrossbowAttackMob, IMai
private static final String FAVORABILITY_TAG = "MaidFavorability";
private static final String SCHEDULE_MODE_TAG = "MaidScheduleMode";
private static final String BACKPACK_DATA_TAG = "MaidBackpackData";
private static final String GAME_SKILL_TAG = "MaidGameSkillData";
private static final String STRUCTURE_SPAWN_TAG = "StructureSpawn";
@Deprecated
private static final String BACKPACK_LEVEL_TAG = "MaidBackpackLevel";
Expand Down Expand Up @@ -223,6 +225,7 @@ public class EntityMaid extends TamableAnimal implements CrossbowAttackMob, IMai
private IBackpackData backpackData = null;
private boolean syncTaskDataMaps = false;
private MaidConfigManager configManager = new MaidConfigManager(this.entityData);
private MaidGameRecordManager gameRecordManager = new MaidGameRecordManager(this);

/**
* 女仆现在可以在前哨站生成,那么会打上这个标签
Expand Down Expand Up @@ -273,14 +276,17 @@ protected void defineSynchedData() {
this.entityData.define(BACKPACK_TYPE, EmptyBackpack.ID.toString());
this.entityData.define(BACKPACK_ITEM_SHOW, ItemStack.EMPTY);
this.entityData.define(BACKPACK_FLUID, StringUtils.EMPTY);
this.entityData.define(GAME_SKILL, new CompoundTag());
this.entityData.define(TASK_DATA_SYNC, new CompoundTag());

// 父类构造方法调用此类,就会出现这种初始化混乱的问题
if (this.configManager == null) {
this.configManager = new MaidConfigManager(this.entityData);
}
this.configManager.defineSynchedData();
if (this.gameRecordManager == null) {
this.gameRecordManager = new MaidGameRecordManager(this);
}
this.gameRecordManager.defineSynchedData();
}

@Override
Expand Down Expand Up @@ -385,6 +391,7 @@ public void baseTick() {
this.randomRestoreHealth();
this.onMaidSleep();
this.syncData();
this.gameRecordManager.tick();
}

@Override
Expand Down Expand Up @@ -1057,9 +1064,9 @@ public void addAdditionalSaveData(CompoundTag compound) {
compound.putInt(EXPERIENCE_TAG, getExperience());
compound.putString(SCHEDULE_MODE_TAG, getSchedule().name());
compound.putString(MAID_BACKPACK_TYPE, getMaidBackpackType().getId().toString());
compound.put(GAME_SKILL_TAG, getGameSkill());
compound.putBoolean(STRUCTURE_SPAWN_TAG, this.structureSpawn);
this.configManager.addAdditionalSaveData(compound);
this.gameRecordManager.addAdditionalSaveData(compound);
this.favorabilityManager.addAdditionalSaveData(compound);
this.scriptBookManager.addAdditionalSaveData(compound);
this.schedulePos.save(compound);
Expand Down Expand Up @@ -1128,9 +1135,6 @@ public void readAdditionalSaveData(CompoundTag compound) {
if (compound.contains(EXPERIENCE_TAG, Tag.TAG_INT)) {
setExperience(compound.getInt(EXPERIENCE_TAG));
}
if (compound.contains(GAME_SKILL_TAG, Tag.TAG_COMPOUND)) {
setGameSkill(compound.getCompound(GAME_SKILL_TAG));
}
if (compound.contains(STRUCTURE_SPAWN_TAG, Tag.TAG_BYTE)) {
this.structureSpawn = compound.getBoolean(STRUCTURE_SPAWN_TAG);
}
Expand All @@ -1149,6 +1153,7 @@ public void readAdditionalSaveData(CompoundTag compound) {
}
}
this.configManager.readAdditionalSaveData(compound);
this.gameRecordManager.readAdditionalSaveData(compound);
this.favorabilityManager.readAdditionalSaveData(compound);
this.scriptBookManager.readAdditionalSaveData(compound);
this.schedulePos.load(compound, this);
Expand Down Expand Up @@ -1763,12 +1768,8 @@ public void setInSittingPose(boolean inSittingPose) {
setOrderedToSit(inSittingPose);
}

public CompoundTag getGameSkill() {
return this.entityData.get(GAME_SKILL);
}

public void setGameSkill(CompoundTag gameSkill) {
this.entityData.set(GAME_SKILL, gameSkill, true);
public MaidGameRecordManager getGameRecordManager() {
return gameRecordManager;
}

private CompoundTag getSyncTaskData() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.github.tartaricacid.touhoulittlemaid.entity.passive;

import com.github.tartaricacid.touhoulittlemaid.entity.item.EntitySit;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;

import static com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid.GAME_SKILL;
import static com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid.GAME_STATUE;

public class MaidGameRecordManager {
private static final String GAME_SKILL_TAG = "MaidGameSkillData";
private static final String GOMOKU = "Gomoku";
private static final byte NONE = 0, WIN = 1, LOSE = 2;

private final EntityMaid maid;

public MaidGameRecordManager(EntityMaid maid) {
this.maid = maid;
}

void defineSynchedData() {
maid.getEntityData().define(GAME_SKILL, new CompoundTag());
maid.getEntityData().define(GAME_STATUE, (byte) 0);
}

void addAdditionalSaveData(CompoundTag compound) {
compound.put(GAME_SKILL_TAG, getGameSkill());
}

void readAdditionalSaveData(CompoundTag compound) {
if (compound.contains(GAME_SKILL_TAG, Tag.TAG_COMPOUND)) {
setGameSkill(compound.getCompound(GAME_SKILL_TAG));
}
}

void tick() {
if (!(this.maid.getVehicle() instanceof EntitySit) && getGameStatue() != NONE) {
resetStatue();
}
}

private CompoundTag getGameSkill() {
return maid.getEntityData().get(GAME_SKILL);
}

private void setGameSkill(CompoundTag gameSkill) {
maid.getEntityData().set(GAME_SKILL, gameSkill, true);
}

private byte getGameStatue() {
return maid.getEntityData().get(GAME_STATUE);
}

private void setGameStatue(byte gameStatue) {
maid.getEntityData().set(GAME_STATUE, gameStatue);
}

public int getGomokuWinCount() {
CompoundTag gameSkill = this.getGameSkill();
if (gameSkill.contains(GOMOKU, Tag.TAG_INT)) {
return gameSkill.getInt(GOMOKU);
}
return 0;
}

public void increaseGomokuWinCount() {
CompoundTag gameSkill = this.getGameSkill();
if (gameSkill.contains(GOMOKU, Tag.TAG_INT)) {
gameSkill.putInt(GOMOKU, gameSkill.getInt(GOMOKU) + 1);
} else {
gameSkill.putInt(GOMOKU, 1);
}
this.setGameSkill(gameSkill);
}

public boolean isWin() {
return this.getGameStatue() == WIN;
}

public boolean isLost() {
return this.getGameStatue() == LOSE;
}

public void markStatue(boolean isWin) {
this.setGameStatue(isWin ? WIN : LOSE);
}

public void resetStatue() {
this.setGameStatue(NONE);
}
}
Loading

0 comments on commit 939cb58

Please sign in to comment.