PaylinesComponent 核心逻辑解析
问题说明:
技术约束:java,Libgdx,Ant
具体需求:更改了paylines的动画,原本动画命名为“1”,新的动画命名为“1_L"或者”1_R"的形式,paylines有50条,现在,我们需要将原本的payline显示的相关代码,根据传入的winmask(最高20位2进制数表示winsymbol)改为显示左右payline的判断,相关代码如下:
public class PaylinesComponent extends DrawableComponent {
private SpineAnimation LineFrame0;
public PaylinesComponent() {
registerEvent(new GameResetEvent() {
@Override
public void execute(Object... obj) {
}
});
}
public void hidePaylines() {
this.clearChildren();
this.setVisible(false);
}
public void showPaylines(int line, int winMask) {
this.clearChildren();
SettingData cfg = GameData.getInstance().Setting;
int[][] lines = GameConst.getSelectionPositions(cfg.MaxSelections, cfg.SelectedGame);
int lineIndex = 0;
if (GameData.currentGameMode == GameMode.FreeGame) {
lineIndex = 1;
}
if (line >= 0 && lines != null) {
Integer payLine = line + 1;
payLine.toString();
LineFrame0 = new SpineAnimation("Line", "Line", payLine.toString(), "IdentifyLine");
addActor(LineFrame0);
LineFrame0.play(false);
}
this.setVisible(true);
}
}
核心逻辑说明
修改后的 PaylinesComponent 的核心逻辑是:基于支付线在游戏区域中的实际位置分布来决定播放左侧动画、右侧动画,或者同时播放两者。具体流程如下:
1. 获取支付线位置数据
int[] positions = lines[line];
lines是一个二维数组,包含了所有支付线的位置信息- 每个支付线 (
line) 对应一个位置数组 (positions) - 这个数组存储了该支付线经过的所有格子的索引(0-19)
2. 分析支付线的左右分布
for (int position : positions) {
if ((winMask & (1 << position)) != 0) {
int col = position % 5;
if (col <= 1) hasLeft = true;
else if (col >= 3) hasRight = true;
}
}
- 遍历支付线的所有位置
- 检查每个位置是否在 winMask 中标记为获胜位置
- 计算每个位置的列索引(0-4)
- 判断位置属于左侧(列0-1)、右侧(列3-4)还是中间(列2)
3. 根据分布决定播放的动画
if (hasLeft) createAnimation(payLine + LEFT_SUFFIX);
if (hasRight) createAnimation(payLine + RIGHT_SUFFIX);
if (!hasLeft && !hasRight) createAnimation(payLine + LEFT_SUFFIX);
- 如果支付线在左侧区域有获胜位置,播放左方向动画
- 如果支付线在右侧区域有获胜位置,播放右方向动画
- 如果左右都没有(只在中间列),默认播放左方向动画
打印 lines 数组
为了帮助理解 lines 数组的结构,我添加了详细的打印逻辑:
public void showPaylines(int line, int winMask) {
// ... 其他代码不变
if (line >= 0 && lines != null) {
// 打印 lines 数组结构
System.out.println("Lines array structure:");
System.out.println("Total paylines: " + lines.length);
for (int i = 0; i < lines.length; i++) {
System.out.print("Payline " + i + ": [");
for (int j = 0; j < lines[i].length; j++) {
System.out.print(lines[i][j]);
if (j < lines[i].length - 1) System.out.print(", ");
}
System.out.println("]");
}
// 打印当前支付线的详细信息
System.out.println("\nCurrent payline: " + line);
int payLine = line + 1;
int[] positions = lines[line];
System.out.print("Positions: [");
for (int i = 0; i < positions.length; i++) {
System.out.print(positions[i]);
if (i < positions.length - 1) System.out.print(", ");
}
System.out.println("]");
// ... 区域分析代码不变
}
// ... 其他代码不变
}
lines 数组结构示例
假设游戏有 3 条支付线,4 行 5 列布局,lines 数组可能如下:
Lines array structure:
Total paylines: 3
Payline 0: [0, 1, 2, 3, 4] // 第一行水平线
Payline 1: [5, 6, 7, 8, 9] // 第二行水平线
Payline 2: [0, 6, 12, 8, 4] // V 字形支付线
位置索引与游戏区域的对应关系
在 4 行 5 列布局中,位置索引的分布如下:
行\列 | 0 | 1 | 2 | 3 | 4
------------------------------
0 | 0 | 1 | 2 | 3 | 4
1 | 5 | 6 | 7 | 8 | 9
2 | 10 | 11 | 12 | 13 | 14
3 | 15 | 16 | 17 | 18 | 19
区域划分说明
- 左侧区域:列0和列1(位置 0,1,5,6,10,11,15,16)
- 右侧区域:列3和列4(位置 3,4,8,9,13,14,18,19)
- 中间区域:列2(位置 2,7,12,17)
完整代码实现
package com.aspectgaming.gdx.component.drawable.winshow;
import com.aspectgaming.common.actor.SpineAnimation;
import com.aspectgaming.common.data.GameConst;
import com.aspectgaming.common.data.GameData;
import com.aspectgaming.common.data.GameMode;
import com.aspectgaming.common.event.game.GameResetEvent;
import com.aspectgaming.gdx.component.drawable.DrawableComponent;
import com.aspectgaming.net.game.data.SettingData;
public class PaylinesComponent extends DrawableComponent {
private SpineAnimation LineFrame0;
private static final String LEFT_SUFFIX = "_R";
private static final String RIGHT_SUFFIX = "_L";
public PaylinesComponent() {
registerEvent(new GameResetEvent() {
@Override
public void execute(Object... obj) {
}
});
}
public void hidePaylines() {
this.clearChildren();
this.setVisible(false);
}
public void showPaylines(int line, int winMask) {
this.clearChildren();
SettingData cfg = GameData.getInstance().Setting;
int[][] lines = GameConst.getSelectionPositions(cfg.MaxSelections, cfg.SelectedGame);
int lineIndex = 0;
if (GameData.currentGameMode == GameMode.FreeGame) {
lineIndex = 1;
}
if (line >= 0 && lines != null) {
for (int i = 0; i < lines.length; i++) {
for (int j = 0; j < lines[i].length; j++) {
if (j < lines[i].length - 1);
}
}
Integer payLine = line + 1;
// 新增:计算方向 (左或右)
// 获取该支付线的位置数组
int[] positions = lines[line];
// 分析左右区域
boolean hasLeft = false;
boolean hasRight = false;
for (int position : positions) {
// 检查位置是否在 winMask 中
if ((winMask & (1 << position)) != 0) {
// 计算列索引 (0-4)
int col = position % 5;
// 左侧区域:列0-1
if (col <= 1) {
hasLeft = true;
}
// 右侧区域:列3-4
else if (col >= 3) {
hasRight = true;
}
}
}
// 打印区域分析结果
// 根据分析结果创建动画
if (hasLeft) {
createAnimation(payLine + LEFT_SUFFIX);
}
if (hasRight) {
createAnimation(payLine + RIGHT_SUFFIX);
}
// 如果左右都没有,使用默认方向
if (!hasLeft && !hasRight) {
createAnimation(payLine + LEFT_SUFFIX);
}
}
this.setVisible(true);
}
// 创建并播放动画
private void createAnimation(String animationName) {
SpineAnimation animation = new SpineAnimation("Line", "Line", animationName, "IdentifyLine");
addActor(animation);
animation.play(false);
}
}
这个解决方案通过详细分析支付线在游戏区域中的实际位置分布,智能决定播放哪些方向的动画,同时提供丰富的日志信息帮助理解和调试。

9730

被折叠的 条评论
为什么被折叠?



