无法检查列表

时间:2014-12-20 03:19:26

标签: c# list unity3d

好吧,我似乎正在重新解决这个问题。我所经历的是,我想你可以说我什么时候会消失"一个怪物(玩家杀死了它),以及" respawn"它,它将所有移动重新添加到列表中。例如:

Monster A在他的名单中有2个动作:Scratch&咆哮......怪物A死了......怪物A重生......怪物A现在在他的名单中有4个动作:Scratch&咆哮......和......划痕&咆哮...

这是我的代码:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class MonsterMoves : MonoBehaviour {

 public List<BaseMove> moves;
 public int level;
 public Scratch scratch = new Scratch();
 public Growl growl = new Growl();
 public FireBall fireball = new FireBall();
 public PowerUp powerup = new PowerUp();

 void Start(){
     moves = gameObject.GetComponent<Monster>().monstersMoves;
     level = gameObject.GetComponent<Monster>().level;
 }

 void Update(){
     level = gameObject.GetComponent<Monster>().level;
     SetupMoves(level);
 }

 private void SetupMoves(int level){
     if(level >= 1 && !moves.Contains(scratch)){
         moves.Add(scratch);
     }
     if(level >= 1 && !moves.Contains(growl)){
         moves.Add(growl);
     }
     if(level >= 7 && !moves.Contains(fireball)){
         moves.Add(fireball);
     }
     if(level >= 10 && !moves.Contains(powerup)){
         moves.Add(powerup);
     }

关于我过度看待的任何想法?

回答一些意见:

当怪物死亡时,它的剧本(简称Monster)称之为:

public void SetDead(){
    isAlive = false;
    timeOfDeath = Time.time;

    ReSpawner.deadMonster.Add(this);

    this.gameObject.SetActive(false);
}

这处理&#34; despawning&#34;怪物,实际上没有摧毁它。然后是那个处理重生的脚本:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ReSpawner : MonoBehaviour {

public float spawnDistance = 50.0f;
public float minSpawnDensity = 6f;
public float minSpawnDistance = 20f;
public int respawnDelay = 10;
public static List<Monster> deadMonster = new List<Monster>();

private Vector3 spawnPoint;
private Vector3 lastSpawnPoint = Vector3.zero;

void Update(){
    for(var i = 0; i < deadMonster.Count; i++){
        Monster monster = deadMonster [i];

        float time = Time.time - monster.timeOfDeath;

        if(time > respawnDelay)
        {
            monster.gameObject.SetActive(true);
            monster.isAlive = true;
            monster.gameObject.rigidbody.WakeUp();
            monster.gameObject.GetComponent<Animator>().enabled = true;
            monster.gameObject.GetComponentInChildren<MonsterAI>().enabled = true;
            spawnPoint = new Vector3(Random.Range(0, 2000), Random.Range(0, 2000), Random.Range(0, 2000));
            spawnPoint.y = TerrainHeight(spawnPoint);
            if(!IsInvalidSpawnPoint(spawnPoint, lastSpawnPoint)){
                NavMeshHit closestHit;
                if(NavMesh.SamplePosition(spawnPoint, out closestHit, 500, 1)){
                    spawnPoint = closestHit.position;
                }else{
                    Debug.Log("...");
                }
                monster.gameObject.transform.position = spawnPoint;
                monster.SetupMonster();
                deadMonster.RemoveAt(i);
                i--;
            }
        }
    }
}

private bool IsInvalidSpawnPoint(Vector3 spawnPoint,Vector3 lastSpawnPoint){
    if(spawnPoint.y == Mathf.Infinity || (spawnPoint - lastSpawnPoint).magnitude <= minSpawnDensity){
        return true;
    }else{
        return false;
    }
}

private float TerrainHeight(Vector3 spawnPoint){
    Ray rayUp = new Ray(spawnPoint, Vector3.up);
    Ray rayDown = new Ray(spawnPoint, Vector3.down);
    RaycastHit hitPoint;
    if(Physics.Raycast(rayUp, out hitPoint, Mathf.Infinity)){
        return hitPoint.point.y;
    }
    else if(Physics.Raycast(rayDown, out hitPoint, Mathf.Infinity)){
        return hitPoint.point.y;
    }else{
        return Mathf.Infinity;
    }
}

}

我犹豫是否要发布整个&#34;怪物&#34;脚本,因为它非常广泛。

所以我做了一些改变。现在,无论何时首次创建怪物,以及当它升级时,我都会调用该函数。这是MonsterMoves的新脚本......

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class Moves : MonoBehaviour {

public List<Move> moves;
public List<Move> movesToLearn = new List<Move>();

void Start(){
    moves = gameObject.GetComponent<Monster>().monstersMoves;
}

public void AddMoves(int level, List<Move> moves){
    foreach(Move move in movesToLearn){
        if(level >= move.levelLearned){
            if(!moves.Contains(move)){
                moves.Add(move);
            }
        }
    }
}

}

我在处理怪物所有属性的脚本中调用此函数。如果移动列表中已有移动,我不想再次添加它。但是,它仍然无法正常工作。即使它已经在那里,它仍然会将移动添加到列表中。

1 个答案:

答案 0 :(得分:0)

在你重生之前从列表中删除怪物A!