3D游戏编程设计作业五

1、改进飞碟(Hit UFO)游戏:

游戏内容要求

  1. 按 adapter模式 设计图修改飞碟游戏
  2. 使它同时支持物理运动与运动学(变换)运动

Adapter模式简介

Adapter模式就是将一个类的接口转换为客户希望的另一个接口。该模式可以使得原本由于接口不兼容不能一起工作的可以一起工作。本次作业的实现要同时支持物理运动和运动学运动,故需要为两个运动控制器CCActionManager和PhysisActionManager类创建一个统一的接口作为Adapter。

代码改变

游戏规则更新

本次游戏的大多数代码沿用了上次的代码,为了让游戏的可玩性更强,我首先在原先的基础上改变了规则,新更新的规则如下:

1.飞碟颜色共有黄、黑、红三种,黄色2分,黑色3分,红色5分,黑色飞碟在round2后才会出现,红色飞碟在round3后才会出现

改变DiskFactory代码如下:

if(random_num > 6 && round >= 3){
            _score = 5;
			random_start =  new Vector3(Random.Range(-80, -90), Random.Range(30, 70), 0);
			_color = Color.red;
		}
        else if(random_num <= 6 && random_num > 3 && round >= 2){
			_score = 3;
			random_start = new Vector3(Random.Range(-80, -100), Random.Range(30,70), 0);
            _color = Color.black;
		}
        else {
			_score = 2;
			random_start = new Vector3(Random.Range(-80, -120), Random.Range(30,90), 0);
			_color = Color.yellow; 
		}

 

2.增加生命值设定,如果飞碟飞出视野,在UserGUI类中扣除生命值,生命值为0游戏结束

public void ReduceBlood(){
        if(blood > 0)
            blood--;
    }

 

 

新增代码

新增IActionManager接口作为Adapter接口,原先存在的运动学运动管理器CCActionManager类和新增的物理学运动管理器PhysicalActionManager都需要实现这个接口,这样场记只需要调用该接口使飞碟运动就实现了同时兼容两种运动,该接口只需要需要实现飞碟运动的方法,即上次代码中UFOfly的方法,代码如下

public interface IActionManager{
        void UFOfly(GameObject disk);
}

修改CCActionManager类让其继承适配器接口

public class CCFlyActionManager : SSActionManger,ISSActionCallback,IActionManager {

    public SSActionEventType Complete = SSActionEventType.Competeted;
    
	public void SSActionEvent(SSAction source) {
        Complete = SSActionEventType.Competeted;
        source.gameObject.SetActive(false);
    }

	public void UFOfly(GameObject disk){
		Complete = SSActionEventType.Started;
        CCFlyAction action = CCFlyAction.getAction(disk.GetComponent<Disk>().Speed);
        addAction(disk, action, this);//激活对象,添加动作
	}
}

仿照CCFlyAction添加物理运动类PhysicalAction,对于物理运动,需要给游戏对象添加刚体,刚体添加后物体会自动下坠,相当于重力效果

public class PhysicalAction : SSAction{
    public float speedx;
    private float gravity = 10f;
    // Use this for initialization
    public override void Start(){
        //添加刚体
        if (!this.gameObject.GetComponent<Rigidbody>()){
            this.gameObject.AddComponent<Rigidbody>();
        }
  
        this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * gravity * 0.5f, ForceMode.Acceleration);
        this.gameObject.GetComponent<Rigidbody>().AddForce(new Vector3(speedx, 0, 0), ForceMode.VelocityChange);
    }

    private PhysicalAction(){}
    public static PhysicalAction getAction(float speed){
        PhysicalAction action = CreateInstance<PhysicalAction>();
        action.speedx = speed;
        return action;
    }

    // Update is called once per frame
    override public void Update(){
        if (transform.position.y <= -8)
        {
            Destroy(this.gameObject.GetComponent<Rigidbody>());//移除刚体
            destory = true;
            callback.SSActionEvent(this);//反馈
        }
    }
}

PhysicalActionManager类的实

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值