简体   繁体   English

Java中Pawn的以下代码是否有类似的方法?

[英]Is there a similar approach to the following code from Pawn in Java?

I used to code in Pawn years ago but now I am working with other languages.几年前我曾经在 Pawn 中编码,但现在我正在使用其他语言。 This is a piece of code that allowed me to create 3d arrays with enum.这是一段代码,允许我使用枚举创建 3d arrays。

enum healthHack
{
    Float:acHealth,
    bool:hImmune,
    bool:aImmune,
    Float:acArmour,
    hcTimer,
    pTick,
    bool:afkImmune,
    bool:hasSpawned
};

new hcInfo[MAX_PLAYERS][healthHack];

Assuming the player_id is 5, MAX_PLAYERS is 500 and while accessing it, I would be able to do it like,假设 player_id 是 5,MAX_PLAYERS 是 500 并且在访问它时,我可以这样做,

hcInfo[player_id][hasSpawned] = false;

hcInfo[player_id][acHealth] = 100;

I was wondering if Java has a similar approach to 3d arrays like this?我想知道 Java 是否有与 3d arrays 类似的方法?

It's better practice in Java to create a class representing a Player and then updating it's values.在 Java 中创建一个代表Player的 class 然后更新它的值是更好的做法。 You could use a Map<PlayerId, Player> to get the player with a specific playerId.您可以使用Map<PlayerId, Player>来获取具有特定 playerId 的玩家。

public class Player {

    private String playerId;
    private double acHealth;
    private boolean hImmune;
    private boolean aImmune;
    private double acArmour;
    private int hcTimer; // What type? I'm just guessing int.
    private int pTick;
    private boolean afkImmune;
    private boolean hasSpawned;

    // Constructor
    // Getters and setters
    // toString, hashCode, equals (needed for hashmap)
}

Map<String, Player> playerMap = new HashMap<>();
playerMap.add("1", player1);

Player player = playerMap.get("1");
if (player != null) {
    player.setHasSpawned(false);
    player.setAcHealth(100.0);
}

C# implementation using classes and Dictionary to allow for lookups by the player id C# 实现使用类和字典来允许通过玩家 ID 进行查找

public class Player 
{
    public int Id {get; set;} //you seem to use int here
    public double AcHealth {get; set;}
    public boolean HImmune {get; set;}
    public boolean AImmune {get; set;}
    public double AcArmour {get; set;}
    public Timer HcTimer {get; set;} //Maybe should look into System.Timers.Timer
    public int PTick {get; set;}
    public boolean AfkImmune {get; set;}
    public boolean HasSpawned {get; set;}
}

Dictionary<int, Player> players = new Dictionary<int, Player>
{
    [5] = new Player 
    {
        //init fields here
    },
    //... more players
};

Player? p1 = players[5];
if (p1 is not null)
{
    p1.HasSpawned = false;
    p1.AcHealth = 100.0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM