예제 #1
0
    private void MovePokemons(Trainer trainer)
    {
        List <Vector2Int>    pokemonIndexes = new List <Vector2Int>();
        List <Vector2Int>    distnacesBetweenTargetPokemon = new List <Vector2Int>();
        List <MoveDirection> previousMoveDirections        = new List <MoveDirection>();
        List <Pokemon>       toMovePokemons = new List <Pokemon>();

        Dictionary <Pokemon, Vector2Int> toMovePokemonsAndIndexes = trainer == chessBoard.owner ? liveOwnerPokemons : liveChallengerPokemons;
        Dictionary <Pokemon, Vector2Int> targetPokemonsAndIndexes = trainer == chessBoard.owner ? liveChallengerPokemons : liveOwnerPokemons;

        foreach (KeyValuePair <Pokemon, Vector2Int> pokemonAndIndex in toMovePokemonsAndIndexes)
        {
            Pokemon pokemon = pokemonAndIndex.Key;
            if (pokemon.currentState != PokemonState.Move || !pokemon.attackTarget.isAlive || pokemon.CheckParalysis())
            {
                pokemonPreviousMove[pokemon] = MoveDirection.None;
                continue;
            }

            Vector2Int index    = pokemonAndIndex.Value;
            Vector2Int distance = targetPokemonsAndIndexes[pokemon.attackTarget] - index;

            pokemonIndexes.Add(pokemonAndIndex.Value);
            distnacesBetweenTargetPokemon.Add(distance);
            previousMoveDirections.Add(pokemonPreviousMove[pokemon]);

            toMovePokemons.Add(pokemon);
        }

        NativeArray <MoveDirection> resultMoveDirections = new NativeArray <MoveDirection>(pokemonIndexes.Count, Allocator.TempJob);
        NativeArray <Vector2Int>    nativePokemonIndexes = new NativeArray <Vector2Int>(pokemonIndexes.ToArray(), Allocator.TempJob);
        NativeArray <Vector2Int>    nativeDistancesBetweenTargetPokemon = new NativeArray <Vector2Int>(distnacesBetweenTargetPokemon.ToArray(), Allocator.TempJob);
        NativeArray <MoveDirection> nativePreviousMoveDirections        = new NativeArray <MoveDirection>(previousMoveDirections.ToArray(), Allocator.TempJob);
        NativeArray <Vector2Int>    nativePokemonsInBattleIndexs        = new NativeArray <Vector2Int>(liveOwnerPokemons.Values.Concat(liveChallengerPokemons.Values).ToArray(), Allocator.TempJob);

        CalculateMoveDirectionJob calculateMoveDirectionJob = new CalculateMoveDirectionJob();

        calculateMoveDirectionJob.resultMoveDirections    = resultMoveDirections;
        calculateMoveDirectionJob.pokemonIndexes          = nativePokemonIndexes;
        calculateMoveDirectionJob.distancesBetweenTarget  = nativeDistancesBetweenTargetPokemon;
        calculateMoveDirectionJob.previousMoveDirections  = nativePreviousMoveDirections;
        calculateMoveDirectionJob.pokemonsInBattleIndexes = nativePokemonsInBattleIndexs;

        JobHandle calculateMoveDirectionHandle = calculateMoveDirectionJob.Schedule(nativePokemonIndexes.Length, 1);

        calculateMoveDirectionHandle.Complete();

        for (int i = 0; i < toMovePokemons.Count; i++)
        {
            Vector2Int    index         = pokemonIndexes[i];
            MoveDirection moveDirection = resultMoveDirections[i];
            Pokemon       toMovePokemon = toMovePokemons[i];

            if (toMovePokemon.transform.position.x > toMovePokemon.attackTarget.transform.position.x)
            {
                toMovePokemon.spriteRenderer.flipX = false;
            }
            else
            {
                toMovePokemon.spriteRenderer.flipX = true;
            }

            pokemonsInBattle[index.x, index.y] = null;

            Vector2Int moveTo = index;
            switch (moveDirection)
            {
            case MoveDirection.Up:
                moveTo += new Vector2Int(0, 1);
                break;

            case MoveDirection.Down:
                moveTo += new Vector2Int(0, -1);
                break;

            case MoveDirection.Right:
                moveTo += new Vector2Int(1, 0);
                break;

            case MoveDirection.Left:
                moveTo += new Vector2Int(-1, 0);
                break;

            default:
                break;
            }

            if (toMovePokemon.trainer == chessBoard.owner ?
                liveOwnerPokemons.Values.Contains(moveTo) :
                liveChallengerPokemons.Values.Contains(moveTo))
            {
                moveDirection = MoveDirection.None;
                moveTo        = index;
            }

            pokemonPreviousMove[toMovePokemon] = moveDirection;
            if (trainer == chessBoard.owner)
            {
                liveOwnerPokemons[toMovePokemon] = moveTo;
            }
            else
            {
                liveChallengerPokemons[toMovePokemon] = moveTo;
            }
            pokemonsInBattle[moveTo.x, moveTo.y] = toMovePokemon;
            toMovePokemon.MoveTo(chessBoard.IndexToWorldPosition(moveTo));
        }

        nativePokemonsInBattleIndexs.Dispose();
        nativePokemonIndexes.Dispose();
        nativePreviousMoveDirections.Dispose();
        nativeDistancesBetweenTargetPokemon.Dispose();
        resultMoveDirections.Dispose();
    }