美文网首页
小白贪狼做塔防(6)

小白贪狼做塔防(6)

作者: 貪狼大人 | 来源:发表于2017-10-23 16:22 被阅读0次

    首先我们创建GameData脚本用来处理玩家的数据

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class GameData : Singleton<GameData> 
    {
        public bool isGameOver = false;
        /// <summary>
        /// 场景中防御塔的数量
        /// </summary>
        public int TowerCount = 0;
        /// <summary>
        /// 玩家ID
        /// </summary>
        public int id = 0;
        /// <summary>
        /// 金钱数
        /// </summary>
        public float money = 300;
        /// <summary>
        /// 分数
        /// </summary>
        public float score = 0;
        /// <summary>
        /// 关卡等级
        /// </summary>
        public int level = 1;
        /// <summary>
        /// 水晶血池
        /// </summary>
        public float HP = 100;
        /// <summary>
        /// 怪物生成频率,游戏难度
        /// </summary>
        private float NanDu = 5;
    
        public void SetData(int id, float money, int level,float score) 
        {
            this.id = id;
            this.money = money;
            this.level = level;
            this.score = score;
            NanDu = NanDu * Mathf.Pow(0.8f,level-1);
            EnemyCreatePos.Instance.CreateSpeed = NanDu;
            GameObject.Find("hp").GetComponent<Text>().text = HP + "";
            GameObject.Find("JinBi").GetComponent<Text>().text = money + "";
            GameObject.Find("level").GetComponent<Text>().text = "LEVEL  " +level + "";
            GameObject.Find("ScoreNum").GetComponent<Text>().text = score + "";
        }
    
        //高塔血量处理
        public void TakeDamage() 
        {
            HP -= 5;     
            if (HP <= 0) 
            {
                HP = 0;
                isGameOver = true;
                GameMode.Instance.GameOver();
            }
            GameObject.Find("hp").GetComponent<Text>().text = HP + "";
        }
    
        //金币处理
        public void AddMoney(float num) 
        {
            money += num;
            GameObject.Find("JinBi").GetComponent<Text>().text = money + "";
        }
    
        public void RemoveMoney(float num) 
        {
            money -= num;
            GameObject.Find("JinBi").GetComponent<Text>().text = money + "";
        }      
            
        //分数处理
        public void DealScore() 
        {
            score += 10;        
            GameObject.Find("ScoreNum").GetComponent<Text>().text = score + "";
            //GameObject.Find("level").GetComponent<Text>().text = "Level  "+level + "";
        }
        //扣分
        public void IncreaseScore()
        {
            score -= 15;
            if (score < 0) 
            {
                score = 0;
            }
            GameObject.Find("ScoreNum").GetComponent<Text>().text = score + "";
        }
    
        //提示信息
    
    }
    

    然后我们创建DataAdpter脚本,用来数据本地化,即在游戏结束后保存信息

    using UnityEngine;
    using System.Data;
    using Mono.Data.Sqlite;
    
    public class DataAdpter : Singleton<DataAdpter>
    {
    
        public  SqliteConnection Con = new SqliteConnection();
        public  SqliteCommand  Cmd = new SqliteCommand();
        public  DataSet Set = new DataSet();
        public  SqliteDataAdapter  Adp;
        public DataAdpter() 
       {
            Con.ConnectionString = "Data Source = dataP.db";
            Con.Open();
            Cmd.Connection = Con;
        }
    
        public void CreateTable() 
        {
            //Cmd.CommandText = "create table message(id int(10),money float(20),level int(10),score float(20))";
            Cmd.CommandText = "create table paihangbang(name char(20),score float(20));";
            //Cmd.CommandText = "create table paihangbang(id INTEGER primary key AUTOINCREMENT,name char(20),score float(20))";
            Cmd.ExecuteNonQuery();
        }
     
        public void Add(string name,float score)
        {       
            Cmd.CommandText = "insert into paihangbang(name,score) values('" + name + "',"  +score+ ")";
            Cmd.ExecuteNonQuery();
        }
        //低于最低分的数据删除
        public void Delete(float lowScore,bool po)
        {
            if (po)
            {
                Cmd.CommandText = "delete from paihangbang where score <='" + lowScore + "';";
            }
            else {
                Cmd.CommandText = "delete from paihangbang where score <'" + lowScore + "';";
            }
            
            Cmd.ExecuteNonQuery();
        }
    
        public void Update(int id,float money,float level,float score)
        {
            //update myte set money = 44 where id = 1;
            //Cmd.CommandText = "update message set money = " + money + ",level = " + level+",score =" +score + " where id = " + id;
            //Cmd.ExecuteNonQuery();
        }
        public ItemData[]  Select()
        {
            Adp = new SqliteDataAdapter ("Select * from paihangbang order by score desc", Con);
            Set = new DataSet();
            Adp.Fill(Set, "paihangbang");
            ItemData[] str = new ItemData[10];
            for (int i = 0; i < str.Length; i++) 
            {
                str[i] = new ItemData();
            }
            if (Set.Tables["paihangbang"].Rows.Count > 0)
            {
                for (int i = 0; i < Set.Tables["paihangbang"].Rows.Count; i++) 
                {
                    str[i].name = Set.Tables["paihangbang"].Rows[i]["name"].ToString();
                    str[i].score =float.Parse(Set.Tables["paihangbang"].Rows[i]["score"].ToString());
                }
                //str[0] = int.Parse(Set.Tables["paihangbang"].Rows[0]["id"].ToString());
                //str[1] = float .Parse(Set.Tables["paihangbang"].Rows[0]["money"].ToString());
                //str[2]=float .Parse(Set.Tables["paihangbang"].Rows[0]["level"].ToString());
                //str[3] = float.Parse(Set.Tables["paihangbang"].Rows[0]["score"].ToString());
                return str;
            }
            else
            {
                return null ;
            }
        }
        public bool  isTrue()
        {
            string sqlCommandString = @"USE AdventureWorks SELECT name FROM sys.sysobjects WHERE type='U' ORDER BY name";
            //Adp = new SqliteDataAdapter("select * from sqlite_master where type = 'table' and name = 'login'", Con);
            if (sqlCommandString.Equals(null) )
            { 
          
            return true;
            }
            else
            {
                return false;
            }
    
            Cmd.ExecuteNonQuery();
    
        }
    
    }
    

    我们来更改一些之前存在的小bug及做一些小改动:
    (1)CreatTower脚本:点击图片后,如果点的不是基座,应该清空当前储存的防御塔名字,而不是点了空白后,点击防御塔仍能正常创建。

    添加清空代码

    (2)EnemyCreatePos脚本,增加随关卡增加怪物生成速度变快的代码,提高难度。

    增加难度

    相关文章

      网友评论

          本文标题:小白贪狼做塔防(6)

          本文链接:https://www.haomeiwen.com/subject/oleauxtx.html