基于各种条件分配纹理的代码效率最高的方法是什么?

huangapple go评论75阅读模式
英文:

Most code-efficient way of assigning textures based on varios conditions?

问题

我目前正在使用自己的引擎编写一个游戏。然而,有一个问题,我不确定如何以最高效的方式解决它:
对于游戏中的许多对象,我需要根据各种条件/状态(我正在使用枚举)分配不同的纹理。一个例子:

玩家的纹理需要根据他所看的方向、是否在行走或等待、他的健康状态等进行更改。

一个解决方案是像这样嵌套的 switch 语句:

switch (getState ()) {
    case IDLE:
        switch (getDirection()) {
            case UP:
                setTexture(Textures.PLAYER_IDLE_UP);
                break;
            case RIGHT:
                setTexture(Textures.PLAYER_IDLE_RIGHT);
                break;
            case DOWN:
                setTexture(Textures.PLAYER_IDLE_DOWN);
                break;
            case LEFT:
                setTexture(Textures.PLAYER_IDLE_LEFT);
                break;
        }
        break;
    case WALKING:
        switch (getDirection()) {
            case UP:
                setTexture(Textures.PLAYER_WALKING_UP);
                break;
            case RIGHT:
                setTexture(Textures.PLAYER_WALKING_RIGHT);
                break;
            case DOWN:
                setTexture(Textures.PLAYER_WALKING_DOWN);
                break;
            case LEFT:
                setTexture(Textures.PLAYER_WALKING_LEFT);
                break;
        }
        break;
    case RUNNING:
        ...
}

但是随着每个条件的增加,这会变得越来越复杂,如果有稍微不同的状态集(例如,我会实现一个僵尸,他可能无法奔跑或会有其他属性等),我就不得不重写它。

我还考虑过给所有的纹理添加标签,并编写一个算法来尝试找到匹配的纹理,但随着越来越多的纹理/条件,以及所有这些都在经常执行,我认为这种方法会变得极其缓慢。

我真的不知道该怎么办,或者是否有任何可以解决这类问题的数据结构,既能高效编码,又能高性能运行。

英文:

I am currently programming a game using a own engine. However, there is a problem and I am not sure how solve it in the most code-efficient way:
For a lot objects in the game I need to assign different textures based on various conditions / states (I am using enums for that). An example:

The player needs to change its texture based on the direction he is looking at, whether he is walking or waiting, his health state, etc.

One solution would be nested switch-statements like this:

switch (getState ()) {
        case IDLE:
            switch (getDirection()) {
                case UP:
                    setTexture(Textures.PLAYER_IDLE_UP);
                    break;
                case RIGHT:
                    setTexture(Textures.PLAYER_IDLE_RIGHT);
                    break;
                case DOWN:
                    setTexture(Textures.PLAYER_IDLE_DOWN));
                    break;
                case LEFT:
                    setTexture(Textures.PLAYER_IDLE_LEFT);
                    break;
            }
            break;
        case WALKING:
            switch (getDirection()) {
                case UP:
                    setTexture(Textures.PLAYER_WALKING_UP);
                    break;
                case RIGHT:
                    setTexture(Textures.PLAYER_WALKING_RIGHT);
                    break;
                case DOWN:
                    setTexture(Textures.PLAYER_WALKING_DOWN);
                    break;
                case LEFT:
                    setTexture(Textures.PLAYER_WALKING_LEFT);
                    break;
            }
            break;
        case RUNNING:
            ...
    }

But this would get more and more complicated with every condition and I would have to rewrite it if there was a slightly other set of states (e.g. I would implement a Zombie and he could not run or would have additional properties etc.).

I also thought of giving all my textures tags and writing an algorithm which tries to find a matching texture but with more and more textures / conditions and all of this being executed very often I think this approach would be extremly slow.

I do not really know what to do or if I am missing any data structures which are adressing these kinds of problems and are both code-efficient and performant.

答案1

得分: 1

我不确定这是否会使其更高效,但肯定会使其对未来的更改更加稳健:
我在考虑像查找表这样的东西。这可以是多维数组,甚至是您自己的数据结构。

您可以将状态和方向编码为整数,例如:

public static final int IDLE = 0;
public static final int WALKING = 1;
...
public static final int UP = 0;
public static final int LEFT = 1;
...

然后假设您有一个包含所有纹理的二维数组。您可以使用getState()getDirection()方法作为数组的索引。因此,您的代码可能类似于:

setTexture(texture_array[getState()][getDirection()]);

当然,您必须在某个时候将所有纹理硬编码到数组中,有些单元格可能必须设置为null或类似值(比如当您不希望在特定的状态-方向组合下设置纹理时),但我认为这样做会使代码看起来更好,并使其对未来的更改更加稳健。

英文:

I'm not sure if it would make it more efficient, but it would definitely make it more robust for future changes:
I was thinking of something like a lookup-table. This could be a multidimensional array, or even your own data structure.

You could code your states and directions as Integers, e.g.

public static final int IDLE = 0;
public static final int WALKING = 1;
...
public static final int UP = 0;
public static final int LEFT = 1;
...

And then let's say you have an 2-dimensional array containing all the textures. You could use getState() and getDirection() methods as indices for the array. So your code could become something similar to:

setTexture(texture_array[getState()][getDirection()]);

Of course you would have to hardcode all your textures into the array at some point, and some cells you might have to set to null or similar (like when you don't want a texture to be set at a certain state-direction-combination), but I think it would look better and make it more robust for future changes.

huangapple
  • 本文由 发表于 2020年4月7日 02:58:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61067033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定