3D游戏设计 homework3

1、简答并用程序验证

1.1 游戏对象运动的本质是什么?

游戏对象运动本质就是在每一帧改变游戏对象的空间属性(位置、欧拉角、比例),并展现出来。

1.2 请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)

(1)修改Transform属性:利用transform改变position来实现抛物线运动,根据物理的运动学知识,只要水平方向的移动速度是不变的,竖直方向有一定的加速度变化的,两个方向的运动矢量相加便是抛物线运动。具体实现:

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

public class movement1 : MonoBehaviour
{
    public float speed;
    // Start is called before the first frame update
    void Start()
    {
        speed = 0;
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.position += Vector3.left * Time.deltaTime * 4;
        this.transform.position += speed / 10 * Vector3.down * Time.deltaTime;
        ++speed;
    }
}

(2)使用向量Vector3:定义一个向量Vector3,同时定义该变量的值,其也是竖直方向上是一个均匀增加的数值,水平方向是一个保持不变的数值,然后将游戏对象原本的position属性与该向量相加。具体实现:

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

public class movement2 : MonoBehaviour
{
    public float speed;
    // Start is called before the first frame update
    void Start()
    {
        speed = 0;
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 vec1= new Vector3(-Time.deltaTime * 4, -Time.deltaTime * (speed / 10), 0);
        ++speed;
        this.transform.position += vec1;
    }
}

(3)利用transform的Translate函数:传入一个Vector3变量来改变position,该变量做抛物线运动。具体实现如下:

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

public class movement3 : MonoBehaviour
{
    public float speed;
    // Start is called before the first frame update
    void Start()
    {
        speed = 0;
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 vec1= new Vector3(-Time.deltaTime * 4, -Time.deltaTime * (speed / 10), 0);
        ++speed;
        transform.Translate(vec1);
    }
}

1.3 写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。

(1)下载太阳系贴图:
下载链接
图片
(2)设置相对位置和贴图:
相对位置
(3)实现自转和公转脚本:

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

public class move : MonoBehaviour
{
    public Transform center;    //各天体公转的圆心
    public float globalSpeed;        //公转速度
    public float selfSpeed;        //自转速度
    public float ry, rz;        //公转平面所在的轴

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 axis = new Vector3(0, ry, rz);     //公转轴
        this.transform.RotateAround(center.position, axis, globalSpeed * Time.deltaTime);   //公转
        this.transform.Rotate(Vector3.up * selfSpeed * Time.deltaTime);       //自转
    }
}

通过设置公转轴使得不在同一个法平面公转,调整速度使得公转速度不一致。
效果

2、编程实践

阅读以下游戏脚本

Priests and Devils
Priests and Devils is a puzzle game in which you will help the Priests and Devils to cross the river within the time limit. There are 3 priests and 3 devils at one side of the river. They all want to get to the other side of this river, but there is only one boat and this boat can only carry two persons each time. And there must be one person steering the boat from one side to the other side. In the flash game, you can click on them to move them and click the go button to move the boat to the other direction. If the priests are out numbered by the devils on either side of the river, they get killed and the game is over. You can try it in many > ways. Keep all priests alive! Good luck!

程序需要满足的要求:

play the game ( http://www.flash-game.net/game/2535/priests-and-devils.html )

(1)列出游戏中提及的事物(Objects):

牧师,恶魔,船,河,两岸

(2)用表格列出玩家动作表(规则表),注意,动作越少越好

动作条件
开船有人物在上面
上船船在对应边且船上有位置 (船只有俩个位置)
下船船在对应边
杀人在船上时牧师人数不大于恶魔或者岸边时牧师人数小于恶魔

(3)请将游戏中对象做成预制;在 GenGameObjects 中创建 长方形、正方形、球 及其色彩代表游戏中的对象。

预制
预制对象的使用:参考官方文档
使用 C# 集合类型 有效组织对象
编程要求:整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
请使用课件架构图编程,不接受非 MVC 结构程序
注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!

(4)分别创建三个脚本Model、View、Controller,其中Model和Controller脚本挂在主摄像机下,View挂在empty下。

  1. Model:实现具体动作及逻辑,同时加载预制对象。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using mygame;

public class Model : MonoBehaviour
{

    Stack<GameObject> start_priests = new Stack<GameObject>();
    Stack<GameObject> end_priests = new Stack<GameObject>();
    Stack<GameObject> start_devils = new Stack<GameObject>();
    Stack<GameObject> end_devils = new Stack<GameObject>();
    
    GameObject[] boat = new GameObject[2];
    GameObject boat_obj;
    public float speed = 50;

    SSDirector one;

    //对象的位置
    Vector3 boatStartPos = new Vector3(-2, 0.25f, 0);
    Vector3 boatEndPos = new Vector3(2, 0.25f, 0);
    Vector3 sideStartPos = new Vector3(-5, 0, 0);
    Vector3 sideEndPos = new Vector3(5, 0, 0);

    Vector3 riverPos = new Vector3(0, -0.25f, 0); 

    float gap = 1.1f;
    Vector3 priestsStartPos = new Vector3(-3.25f, 0.75f, 0);
    Vector3 priestsEndPos = new Vector3(3.25f, 0.75f, 0);
    Vector3 devilsStartPos = new Vector3(-5, 1, 0);
    Vector3 devilsEndPos = new Vector3(5, 1, 0);


    // Start is called before the first frame update
    void Start()
    {
        one = SSDirector.GetInstance();
        one.setModel(this);
        loadSrc();
    }

    // Update is called once per frame
    void Update()
    {
        setpositionS(start_priests, priestsStartPos);
        setpositionE(end_priests, priestsEndPos);
        setpositionS(start_devils, devilsStartPos);
        setpositionE(end_devils, devilsEndPos);

        if (one.state == State.SE_move)
        {
            boat_obj.transform.position = Vector3.MoveTowards(boat_obj.transform.position, boatEndPos, Time.deltaTime * speed);
            if (boat_obj.transform.position == boatEndPos)
            {
                one.state = State.End;
            }
            checkD();
        }
        else if (one.state == State.ES_move)
        {
            boat_obj.transform.position = Vector3.MoveTowards(boat_obj.transform.position, boatStartPos, Time.deltaTime * speed);
            if (boat_obj.transform.position == boatStartPos)
            {
                one.state = State.Start;
            }
            checkD();
        }
        checkV();
    }

    //加载游戏对象
    void loadSrc()
    {
        //sides
        Instantiate(Resources.Load("side"), sideStartPos, Quaternion.identity);
        Instantiate(Resources.Load("side"), sideEndPos, Quaternion.identity);

        //river
        Instantiate(Resources.Load("river"), riverPos, Quaternion.identity);

        //boat
        boat_obj = Instantiate(Resources.Load("boat"), boatStartPos, Quaternion.identity) as GameObject;

        //prisets and devils
        for (int i = 0; i < 3; i++)
        {
            start_priests.Push(Instantiate(Resources.Load("Priest")) as GameObject);
            start_devils.Push(Instantiate(Resources.Load("Devil")) as GameObject);
        }
    }

    //设置起点对象位置
    void setpositionS(Stack<GameObject> aaa, Vector3 pos)
    {
        GameObject[] temp = aaa.ToArray();
        for (int i = 0; i < aaa.Count; i++)
        {
            temp[i].transform.position = pos + new Vector3(-gap * i * 0.5f, 0, 0);
        }
    }

    //设置终点对象位置
    void setpositionE(Stack<GameObject> aaa, Vector3 pos)
    {
        GameObject[] temp = aaa.ToArray();
        for (int i = 0; i < aaa.Count; i++)
        {
            temp[i].transform.position = pos + new Vector3(gap * i * 0.5f, 0, 0);
        }
    }

        //上船
        void getOnTheBoat(GameObject obj)
    {
        obj.transform.parent = boat_obj.transform;
        if (boatNum() != 0)
        {
            if (boat[0] == null)
            {
                boat[0] = obj;
                obj.transform.position = boat_obj.transform.position + new Vector3(0.3f, obj.transform.position.y - 0.25f, 0);
            }
            else
            {
                boat[1] = obj;
                obj.transform.position = boat_obj.transform.position + new Vector3(-0.3f, obj.transform.position.y - 0.25f, 0);
            }
        }
    }
    //判断船上的空位数
    int boatNum()
    {
        int num = 0;
        for (int i = 0; i < 2; i++)
        {
            if (boat[i] == null)
            {
               ++ num;
            }
        }
        return num;
    }

    //船移动
    public void moveBoat()
    {
        if (boatNum() != 2)
        {
            if (one.state == State.Start)
            {
                one.state = State.SE_move;
            }
            else if (one.state == State.End)
            {
                one.state = State.ES_move;
            }
        }
    }

    //下船
    public void getOffTheBoat(int flag)
    {
        if (flag == 0 || flag == 2)
        {
            for (int i = 0; i < 2; ++i)
            {
                if (boat[i] != null && boat[i].tag == "Priest")
                {
                    Debug.Log("enter1");
                    if (flag == 0)
                    {
                        start_priests.Push(boat[i]);
                    }
                    else
                    {
                        end_priests.Push(boat[i]);
                    }
                    boat[i] = null;
                    break;
                }
            }
        }
        else if (flag == 1 || flag == 3)
        {
            for (int i = 0; i < 2; ++i)
            {
                if (boat[i] != null && boat[i].tag == "Devil")
                {
                    if (flag == 1)
                    {
                        start_devils.Push(boat[i]);
                    }
                    else
                    {
                        end_devils.Push(boat[i]);
                    }
                    boat[i] = null;
                    break;
                }
            }
        }
        else
        {
            for (int i = 0; i < 2; ++i)
            {
                if (boat[i] != null)
                {   
                    if (boat[i].tag == "Devil")
                    {
                        start_devils.Push(boat[i]);
                    }
                    else
                        start_priests.Push(boat[i]);
                    boat[i] = null;
                }
            }
        }
    }

    //检查是否失败
    void checkD()
    {
        int bp = 0, bd = 0;
        for (int i = 0; i < 2; i++)
        {
            if (boat[i] != null && boat[i].tag == "Priest")
            {
                bp++;
            }
            else if (boat[i] != null && boat[i].tag == "Devil")
            {
                bd++;
            }
        }

        int sp = start_priests.Count, sd = start_devils.Count, ep = end_priests.Count, ed = end_devils.Count;
        if (one.state == State.Start)
        {
            int sum = sp + bp;
            if ((sum != 0 && sum < (sd + bd)) || (ep != 0 && ep < ed))
                one.state = State.Lose;
        }
        else
        {
            int sum = ep + bp;
            if ((sp != 0 && sp < sd) || (sum != 0 && sum < ed + bd))
                one.state = State.Lose;
        }
    }

    //检查是否胜利
    void checkV()
    {
        if (end_devils.Count == 3 && end_priests.Count == 3)
        {
            one.state = State.Win;
            return;
        }
    }

    //游戏对象从岸上到船上的变化
    public void priS()
    {
        if (start_priests.Count != 0 && boatNum() != 0 && one.state == State.Start)
        {
            getOnTheBoat(start_priests.Pop());
        }
    }
    public void priE()
    {
        if (end_priests.Count != 0 && boatNum() != 0 && one.state == State.End)
        {
            getOnTheBoat(end_priests.Pop());
        }
    }
    public void delS()
    {
        if (start_devils.Count != 0 && boatNum() != 0 && one.state == State.Start)
        {
            getOnTheBoat(start_devils.Pop());
        }
    }
    public void delE()
    {
        if (end_devils.Count != 0 && boatNum() != 0 && one.state == State.End)
        {
            getOnTheBoat(end_devils.Pop());
        }
    }

    //重置游戏
    public void Reset()
    {
        boat_obj.transform.position = boatStartPos;
        one.state = State.Start;

        int num1 = end_devils.Count, num2 = end_priests.Count;
        for (int i = 0; i < num1; i++)
        {
            Debug.Log(i);
            start_devils.Push(end_devils.Pop());
        }

        for (int i = 0; i < num2; i++)
        {
            start_priests.Push(end_priests.Pop());
        }
        if (boatNum() != 2)
        {
            getOffTheBoat(4);
        }
    }
}

  1. View:处收 Input 事件,渲染 GUI ,接收事件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using mygame;

public class View : MonoBehaviour
{

    SSDirector one;
    UserAction action;

     // Start is called before the first frame update
    void Start()
    {
        one = SSDirector.GetInstance();
        action = SSDirector.GetInstance() as UserAction;
    }

    private void OnGUI()
    {
        GUI.skin.label.fontSize = 30;
        if (one.state == State.Win)
        {
            if (GUI.Button(new Rect(650, 140, 300, 100), "WIN\n(click here to reset)"))
            {
                action.reset();
            }
        }
        if (one.state == State.Lose)
        {
            if (GUI.Button(new Rect(650, 140, 300, 100), "LOSE\n(click here to reset)"))
            {
                action.reset();
            }
        }

        if (GUI.Button(new Rect(700, 80, 100, 50), "GO"))
        {
            action.moveBoat();
        }
        if (GUI.Button(new Rect(550, 250, 90, 50), "LPriestsOFF"))
        {
            action.offBoatLP();
        }
        if (GUI.Button(new Rect(950, 250, 90, 50), "RPriestsOFF"))
        {
            action.offBoatRP();
        }
        if (GUI.Button(new Rect(650, 250, 90, 50), "LDevilsOFF"))
        {
            action.offBoatLD();
        }
        if (GUI.Button(new Rect(850, 250, 90, 50), "RDevilsOFF"))
        {
            action.offBoatRD();
        }
        if (GUI.Button(new Rect(350, 130, 75, 50), "PriestsON"))
        {
            action.priestSOnSide();
        }
        if (GUI.Button(new Rect(500, 130, 75, 50), "DevilsON"))
        {
            action.devilSOnSide();
        }
        if (GUI.Button(new Rect(1050, 130, 75, 50), "DevilsON"))
        {
            action.devilEOnSide();
        }
        if (GUI.Button(new Rect(1200, 130, 75, 50), "PriestsON"))
        {
            action.priestEOnSide();
        }
    }
}
  1. Controller:接受用户事件,控制模型的变化。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using mygame;

namespace mygame
{
    public enum State {Start, SE_move, ES_move, End, Win, Lose };

    public interface UserAction
    {
        void priestSOnSide();
        void priestEOnSide();
        void devilSOnSide();
        void devilEOnSide();
        void moveBoat();
        void offBoatLP();
        void offBoatRP();
        void offBoatLD();
        void offBoatRD();
        void reset();
    }

    public class SSDirector : System.Object, UserAction
    {
        private static SSDirector _instance;

        public Controller currentScenceController;
        public State state = State.Start;
        private Model game_obj;

        public static SSDirector GetInstance()
        {
            if (_instance == null)
            {
                _instance = new SSDirector();
            }
            return _instance;
        }

        public Model getModel()
        {
            return game_obj;
        }

        internal void setModel(Model someone)
        {
            if (game_obj == null)
            {
                game_obj = someone;
            }
        }

        public void priestSOnSide()
        {
            game_obj.priS();
        }
        public void priestEOnSide()
        {
            game_obj.priE();
        }
        public void devilSOnSide()
        {
            game_obj.delS();
        }
        public void devilEOnSide()
        {
            game_obj.delE();
        }
        public void moveBoat()
        {
            game_obj.moveBoat();
        }
        public void offBoatLP()
        {
            game_obj.getOffTheBoat(0);
        }
        public void offBoatRP()
        {
            game_obj.getOffTheBoat(2);
        }
        public void offBoatLD()
        {
            game_obj.getOffTheBoat(1);
        }
        public void offBoatRD()
        {
            game_obj.getOffTheBoat(3);
        }
        public void reset()
        {
            game_obj.Reset();
        }
    }
}

public class Controller : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {
        SSDirector one = SSDirector.GetInstance();
    }

    // Update is called once per frame
    void Update()
    {

    }
}

最终效果:
效果
视频链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值