"Learn Unity3d with Me" make a 2D 90 tank battle Map editor

Source: Internet
Author: User

From October 20 to now, Unity3d also learned 10 days, for Unity3d also has a general understanding, it is necessary to do a small game to test their learning results. After two days of hard work, finally it is a can play a battle of tanks. First of all, tell me about my design goals:

1. Map Editor 2. Prop System 3. Simple AI system 4. Scoring device

Among them, the most important is the map editor, followed by the AI system, the other few are quite simple.

--------------------------------------------------------------------------------------------------------------- ---------------------------

Map Editor

My idea is that after you edit the map in the editor, you can play it by opening the game directly. In this case, you need a file to store these map elements. Since XML is very well supported in C #, it's easy to use.

Then we specify the format of the map, first the map is composed of an item, the original item has ordinary walls, sturdy walls, water and grass (Demacia). In order to facilitate the reading, we use a name to specify them, and secondly, since we are 2D scene, so the coordinates of the deposit are X and Y. The last available data storage format is as follows:

<item>      <name>gras</name>      <position_x>-1.2</position_x>      <position_ Y>-3</position_y>    </item>

The entire file is probably the following structure:

<data>  <items>    <item>      <name>gras</name>      <position_x>-1.2 </position_x>      <position_y>-3</position_y>    </item>  </items></ Data>

The corresponding file is stored in the following function:

    BOOL Saveinxml () {if (file.exists (M_sxmlpath)) {file.delete (M_sxmlpath);        } XmlDocument xmldoc = new XmlDocument ();        XmlElement data = xmldoc.createelement ("Data");        Xmldoc.appendchild (data);        Map data XmlElement Xml_items = xmldoc.createelement ("items"); Data.        AppendChild (Xml_items); foreach (Gameobject item in M_items) {Debug.Log ("Data to Save:" + Item.name + "Location:" + item.transform.position            . x + "," + ITEM.TRANSFORM.POSITION.Y);            Save in XML file XmlElement Xml_item = xmldoc.createelement ("item"); Xml_items.            AppendChild (Xml_item);            XmlElement name = xmldoc.createelement ("name"); Name.            InnerText = Item.name; Xml_item.            AppendChild (name);            XmlElement position_x = xmldoc.createelement ("position_x");            Position_x.innertext = Item.transform.position.x.tostring (); Xml_item.         AppendChild (position_x);   XmlElement position_y = xmldoc.createelement ("position_y");            Position_y.innertext = Item.transform.position.y.tostring (); Xml_item.        AppendChild (position_y);        } xmldoc.save (M_sxmlpath);        Debug.Log ("Create XML Complete");        M_items.clear ();    return true; }

Then there is the corresponding read function (read the direct instantiation display):

    void Readmap (XmlNode xml_items) {xmlnodelist items = Xml_items.        SelectNodes ("item"); foreach (XmlNode current_node in items) {XmlNode Current_name = Current_node.            selectSingleNode ("name"); XmlNode position_x = Current_node.            selectSingleNode ("position_x"); XmlNode position_y = Current_node.            selectSingleNode ("position_y"); Vector3 psition = new Vector3 (float. Parse (Position_x.innertext), float.            Parse (Position_y.innertext),-1); if (current_name. InnerText = = "Wall") {M_readitem = Instantiate (M_itemwall, Psition, quaternion.identity) as Gam            Eobject; } else if (current_name. InnerText = = "Gras") {M_readitem = Instantiate (M_itemgras, Psition, quaternion.identity) as Gam            Eobject; } else if (current_name. InnerText = = "Steel") {M_readitem = Instantiate (M_itemsteel, Psition, quaternion.identity) as Gameobject; } else if (current_name. InnerText = = "Water") {M_readitem = Instantiate (M_itemwater, Psition, quaternion.identity) as G            Ameobject;            } else continue; M_readitem.name = Current_name.            InnerText; Debug.Log ("Data to read:" + m_readitem.name + "Location:" + m_readitem.transform.position.x + "," + m_readitem.transform.position.y)            ;        M_items.add (M_readitem); }    }

This completes the core part of a basic map editor, and the rest is a drag-and-drop control.

With regard to the drag-and-drop control, the idea is that the item on the map is on the right, then clicking on item will copy a click to the item, and then you can drag the copied item to the map, and here is the implementation code:

    void Update () {//detection of the left mouse button pickup if (Input.getmousebuttondown (0)) {//mouse screen coordinate space position turn ray            M_ray = Camera.main.ScreenPointToRay (input.mouseposition);            M_rayhit = Physics2d.getrayintersection (M_ray); X-ray detection, related detection information saved to the RAYCASTHIT structure if (m_rayhit) {//Print Ray collision to the name of the object Deb Ug.                Log (M_rayhit.collider.gameObject.name);                    if (M_rayhit.collider.gameObject.name = = "Walls" | |                    M_rayhit.collider.gameObject.name = = "Grass" | |                    M_rayhit.collider.gameObject.name = = "Steels" | | M_rayhit.collider.gameObject.name = = "Waters") {M_clickitem = instantiate (m_rayhit.coll                    Ider.gameobject, M_rayhit.collider.transform.position, quaternion.identity) as Gameobject;       M_clickitem.name = m_rayhit.collider.gameObject.name.Substring (0, m_rayhit.collider.gameobject.name.length-1);             M_clickitem.tag = M_clickitem.name;                Error:not define tag in Editorm_items.add (M_clickitem);                    } else if (M_rayhit.collider.gameObject.name = = "Wall" | |                    M_rayhit.collider.gameObject.name = = "Gras" | |                    M_rayhit.collider.gameObject.name = = "Steel" | | M_rayhit.collider.gameObject.name = = "Water") {M_clickitem = M_rayhit.collider.gameObje                Ct                } else {m_clickitem = null;            }} else {m_clickitem = null; }} if (Input.getmousebutton (0)) {if (M_clickitem! = null) {m_ ClickItem.transform.position = Setpointinmap (new Vector3 (camera). Screentoworldpoint (input.mouseposition). x, Camera.            Screentoworldpoint (input.mouseposition). Y,-1)); }} if (input.geTmousebuttonup (0)) {if (M_clickitem! = null) {Vector3 Cur_point = Setpointin Map (new Vector3 (camera). Screentoworldpoint (input.mouseposition). x, Camera.                Screentoworldpoint (input.mouseposition). Y,-1)); if (M_Map.collider2D.OverlapPoint (Cur_point)) {m_clickItem.transform.position = Cur_poi                nt                } else//Destroy {Destroy (M_clickitem) out of the map; }            }        }    }



This completes the basic functionality of a simple map editor.

"Learn Unity3d with Me" make a 2D 90 tank battle Map editor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.