欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

3D游戏设计作业3

程序员文章站 2022-07-13 09:57:20
...

牧师与魔鬼 动作分离版
【2019新要求】:设计一个裁判类,当游戏达到结束条件时,通知场景控制器游戏结束

动作分离版在上一次作业的基础上加以改进,增加了几个关键类:
SSActionCallback类定义了事件处理接口。CCSequenceAction类组合动作实现,循环保存动作序列。SSActionManager类管理动作之间的切换。SceneActionManager类实现对当前场景的直接管理。SSAction类是所有动作的基类。CCMoveToAction类实现简单的动作。

具体代码如下:

SSActionCallback类:

public interface ISSActionCallback{
	void SSActionEvent(SSAction source);
}

CCSequenceAction类

public class CCSequenceAction:SSAction,ISSActionCallback{
	public List<SSAction> sequence;
	public int repeat = 1;
	public int index = 0;
	public static CCSequenceAction getAction(int repeat,int index,List<SSAction>sequence){
		CCSequenceAction action = ScriptableObject.CreateInstance<CCSequenceAction>();
		action.sequence = sequence;
		action.repeat = repeat;
		action.index = index;
		return action;
	}

	public void SSActionEvent(SSAction source){
		source.destory = false;
		this.index ++;
		if(this.index >= sequence.Count){
			this.index = 0;
			if(repeat>0)
				repeat --;
			if(repeat == 0){
				this.destory = true;
				this.callback.SSActionEvent(this);
			}
		}
	}


	public override void Update(){
        if (sequence.Count == 0) return;
        if (index < sequence.Count){
            sequence[index].Update();
        }
    }

	public override void Start(){
        foreach (SSAction action in sequence){
            action.gameObject = this.gameObject;
            action.transform = this.transform;
            action.callback = this;
            action.Start();
        }
    }
	void OnDestory(){
		foreach(SSAction action in sequence){
			DestroyObject(action);
		}
	}
}

SSAction类

public class SSAction : ScriptableObject {

	public bool enable = true; 
	public bool destory = false;
	public GameObject gameObject;
	public Transform transform; 
	public ISSActionCallback callback; 
	
	protected SSAction(){} 
	
	public virtual void Start () {
		throw new System.NotImplementedException();
	}
	
	
	public virtual void Update () {
		throw new System.NotImplementedException();
	}
}

SSActionManager类

public class SSActionManger:MonoBehaviour{
	private Dictionary<int,SSAction> actions = new Dictionary<int, SSAction>();
	private List<SSAction> waitingToAdd = new List<SSAction>();
	private List<int> waitingToDelete = new List<int>();

	protected void Update(){
		foreach (SSAction ac in waitingToAdd){
			actions[ac.GetInstanceID()] = ac;
		}
		waitingToAdd.Clear();
		foreach (KeyValuePair<int,SSAction> kv in actions){
			SSAction ac = kv.Value;
			if(ac.destory){
				waitingToDelete.Add(ac.GetInstanceID());
			}
			else if (ac.enable){
				ac.Update();
			}
		}
		foreach(int key in waitingToDelete){
				SSAction ac = actions[key];
				actions.Remove(key);
				DestroyObject(ac);
		}
		waitingToDelete.Clear();
	}
	
	public void addAction(GameObject gameObject,SSAction action, ISSActionCallback callback){
		action.gameObject = gameObject;
		action.transform = gameObject.transform;
		action.callback = callback;
		waitingToAdd.Add(action);
		action.Start();
	}
}

SceneActionManager类

public class SceneActionManager:SSActionManger,ISSActionCallback{
	public SSActionEventType complete = SSActionEventType.Competeted;
	public CoastModel startCoast;
    public CoastModel endCoast;
    public BoatModel boat;

	Judge judge;

	
	public void getFirstControllerRessoruce(CoastModel start, CoastModel end, BoatModel b){
		startCoast = start;
		endCoast = end;
		boat = b;
		judge = new Judge(startCoast,endCoast,boat);
	}

	public int getGameState(){
		return judge.CheckGameState();
	}

	public void Boatmove(){
		complete = SSActionEventType.Started;
		CCMoveToAction action = CCMoveToAction.getAction(boat.moveBoatPos(),boat.move_speed);
		addAction(boat.getBoat(),action,this);
	}

	public void move(RoleModel role,Vector3 dest){
		
		Vector3 CurrentPos = role.getRolePos();
        Vector3 MiddlePos = CurrentPos;
        if (dest.y > CurrentPos.y){
            MiddlePos.y = dest.y;
        }
        else{
            MiddlePos.x = dest.x;
        }
        SSAction action1 = CCMoveToAction.getAction(MiddlePos, role.move_speed);
        SSAction action2 = CCMoveToAction.getAction(dest, role.move_speed);
        SSAction seqAction = CCSequenceAction.getAction(1, 0, new List<SSAction> { action1, action2 });
        this.addAction(role.getRole(), seqAction, this);
	}

CCMoveToAction类

public class CCMoveToAction:SSAction{
	public Vector3 target;
	public float speed;
	private CCMoveToAction(){}
	public static  CCMoveToAction getAction(Vector3 target, float speed){
		CCMoveToAction action = ScriptableObject.CreateInstance<CCMoveToAction>();
		action.target = target;
		action.speed = speed;
		return action;
	}

	public override void Update(){
		this.transform.position = Vector3.MoveTowards(transform.position,target,speed*Time.deltaTime);
		if(transform.position == target){
			destory = true;
			callback.SSActionEvent(this);
		}
	}

	
	public override void Start(){

	}
}

游戏截图:
3D游戏设计作业3