10个游戏案例总结

1.拼图游戏开始位置是正确位置。

2.设置很多状态枚举,举明当前状态和下一个状态,在update里面开始swich case 指明状态和下一个状态。

3.状态迁移还有初始化的处理啊

    // 状态迁移时的初始化处理

        while(this.next_step != STEP.NONE) {

            this.step = this.next_step;

            this.next_step = STEP.NONE;

            switch(this.step) {

                case STEP.IDLE:
                {
                    this.SetHeightOffset(this.height_offset);
                }
                break;

                case STEP.RESTART:
                {
                    this.transform.position = this.start_position;

                    this.SetHeightOffset(this.height_offset);

                    this.next_step = STEP.IDLE;
                }
                break;

                case STEP.DRAGING:
                {
                    this.begin_dragging();

                    // 将拖动开始事件通知给拼图管理类
                    this.pazzle_control.PickPiece(this);

                    this.game_control.playSe(GameControl.SE.GRAB);
                }
                break;

                case STEP.FINISHED:
                {
                    // 靠近正解位置不远处松开
        
                    // 吸附到正解位置
                    this.transform.position = this.finished_position;
        
                    // 通知拼图管理类将这个碎片放置到正解的位置
                    this.pazzle_control.FinishPiece(this);

                }
                break;
            }
        }

4.3d焦点的处理

// 将鼠标的位置,变换为3D空间内的世界坐标
    //
    // ・穿过鼠标光标和摄像机位置的直线
    // ・ 用于判定是否和地面碰撞的平面
    // 求出↑二者的交点
    //
    public bool        unproject_mouse_position(out Vector3 world_position, Vector3 mouse_position)
    {
        bool    ret;
        float    depth;

        // 通过碎片中心的水平(法线为Y轴,XZ平面)面
        Plane    plane = new Plane(Vector3.up, new Vector3(0.0f, this.transform.position.y, 0.0f));

        // 穿过摄像机位置和鼠标光标位置的直线
        Ray        ray = this.game_camera.GetComponent<Camera>().ScreenPointToRay(mouse_position);

        // 求出上面二者的交点

        if(plane.Raycast(ray, out depth)) {

            world_position = ray.origin + ray.direction*depth;

            ret = true;

        } else {

            world_position = Vector3.zero;

            ret = false;
        }

        return(ret);
    }

5.制定了绘制顺序

  1. private void    set_height_offset_to_pieces()
        {
            float    offset = 0.01f;
    
            int        n = 0;
    
            foreach(PieceControl piece in this.active_pieces) {
    
                if(piece == null) {
    
                    continue;
                }
    
                // 指定绘制的顺序
                // pieces 中越前面的碎片=越靠近上方的碎片被绘制的顺序越晚
                //
                piece.GetComponent<Renderer>().material.renderQueue = this.GetDrawPriorityPiece(n);
    
                // 为了能够使点击时位于最上方的碎片的 OnMouseDown() 方法被调用,
                // 指定Y轴高度的偏移
                // (不这样处理的话可能会由于绘制优先度的关系而导致下面的碎片响应了点击)
    
                offset -= 0.01f/this.piece_num;
    
                piece.SetHeightOffset(offset);
    
                //
    
                n++;
            }
        }

猜你喜欢

转载自www.cnblogs.com/xiaomao21/p/9441932.html