Exemplo n.º 1
0
    private Rule applyRule(RuleKey ruleKey, LinkKey linkKey, ElementScript firstElement, ElementScript secondElement)
    {
        Rule rule;

        if (rules.TryGetValue(ruleKey, out rule))
        {
            // there is a rule between these two types
            if (rule.hasLink)
            {
                Debug.Log("Adding link for rule " + rule.type + " between " + firstElement.name + " and " + secondElement.name);
                addLink(rule.type, linkKey, firstElement, secondElement);
            }
            else
            {
                // remove any link between these two objects
                Debug.Log("Removing link for rule " + rule.type + " between " + firstElement.name + " and " + secondElement.name);
                removeLink(linkKey, firstElement, secondElement);
            }

            firstElement.Type  = rule.firstTypeFinal;
            secondElement.Type = rule.secondTypeFinal;

            UpdateLinks(linkKey);
        }

        return(rule);
    }
Exemplo n.º 2
0
    private Link addLink(string ruleType, LinkKey linkKey, ElementScript firstElement, ElementScript secondElement)
    {
        Link link;

        // if link already exists update it with the new rule type
        if (links.TryGetValue(linkKey, out link))
        {
            if (link.type != ruleType)
            {
                activeRules[link.type].Remove(linkKey);
                link.type = ruleType;
                activeRules[ruleType].Add(linkKey);
            }

            return(link);
        }

        link           = new Link(ruleType, firstElement, secondElement);
        links[linkKey] = link;

        HashSet <LinkKey> linksForRule;

        if (!activeRules.TryGetValue(ruleType, out linksForRule))
        {
            linksForRule = new HashSet <LinkKey>();
            activeRules.Add(ruleType, linksForRule);
        }

        linksForRule.Add(linkKey);

        HashSet <LinkKey> neighborsFirst;

        if (!elementLinks.TryGetValue(firstElement.name, out neighborsFirst))
        {
            neighborsFirst = new HashSet <LinkKey>();
            elementLinks.Add(firstElement.name, neighborsFirst);
        }

        neighborsFirst.Add(linkKey);

        HashSet <LinkKey> neighborsSecond;

        if (!elementLinks.TryGetValue(secondElement.name, out neighborsSecond))
        {
            neighborsSecond = new HashSet <LinkKey>();
            elementLinks.Add(secondElement.name, neighborsSecond);
        }

        neighborsSecond.Add(linkKey);

        return(link);
    }
Exemplo n.º 3
0
    private void removeLink(LinkKey linkKey, ElementScript firstElement, ElementScript secondElement)
    {
        Link link;

        if (links.TryGetValue(linkKey, out link))
        {
            links.Remove(linkKey);
            link.Remove();
        }

        elementLinks[firstElement.name].Remove(linkKey);
        elementLinks[secondElement.name].Remove(linkKey);
    }
Exemplo n.º 4
0
    public void AddCollision(ElementScript firstElement, ElementScript secondElement)
    {
        // see if we already have a link between these two objects
        LinkKey linkKey = new LinkKey(firstElement.name, secondElement.name);

        if (!links.ContainsKey(linkKey))
        {
            Rule rule;
            if ((rule = applyRule(new RuleKey(firstElement.Type, secondElement.Type), linkKey, firstElement, secondElement)) == null)
            {
                rule = applyRule(new RuleKey(secondElement.Type, firstElement.Type), linkKey, secondElement, firstElement);
            }
        }
    }
Exemplo n.º 5
0
    // Update is called once per frame
    void Update()
    {
        fliedDis += moveSpeed * Time.deltaTime;
        this.transform.position += ShootDirection * moveSpeed * Time.deltaTime;


        Collider[]        PoweredColliders = Physics.OverlapSphere(transform.position, hitRange);
        List <GameObject> ColliderList     = new List <GameObject>();

        foreach (Collider collider in PoweredColliders)
        {
            if (collider.gameObject.tag == "Element" && !collider.isTrigger)
            //if (collider.gameObject.tag != "Ground" && !collider.isTrigger && collider.gameObject.tag != "Player")
            {
                MeshRenderer  mr = collider.gameObject.GetComponent <MeshRenderer>();
                ElementScript es = collider.gameObject.GetComponent <ElementScript>();
                if (es)
                {
                    ColliderList.Add(collider.gameObject);
                    //ElementColor = mr.material.color;
                }
                else
                {
                    ElementScript parent_es = collider.transform.parent.gameObject.GetComponent <ElementScript>();
                    if (parent_es)
                    {
                        ColliderList.Add(parent_es.gameObject);
                    }
                }
            }
        }

        foreach (GameObject collider in ColliderList)
        {
            ElementScript es = collider.gameObject.GetComponent <ElementScript>();

            //es.SetPowerElement(PowerElement);
            bool canSet = es.SetPowerElement(PowerElement, BallValue);
            if (canSet)
            {
                Destroy(this.gameObject);
            }
            return;
        }

        if (fliedDis >= ShootRange)
        {
            Destroy(this.gameObject);
        }
    }
Exemplo n.º 6
0
    // Update is called once per frame
    void Update()
    {
        SwordElement = player.GetElementType();
        boundTrans   = SwordObject.transform;

        if (AttackValue == 0)
        {
            return;
        }

        PowerRenderer.material.color = ElementDefine.GetElementColor(SwordElement);
        Collider[]        PoweredColliders = Physics.OverlapBox(boundTrans.position, boundTrans.localScale);
        List <GameObject> ColliderList     = new List <GameObject>();

        foreach (Collider collider in PoweredColliders)
        {
            if (collider.gameObject.tag == "Element" && !collider.isTrigger)
            //if (collider.gameObject.tag != "Ground" && !collider.isTrigger && collider.gameObject.tag != "Player")
            {
                MeshRenderer  mr = collider.gameObject.GetComponent <MeshRenderer>();
                ElementScript es = collider.gameObject.GetComponent <ElementScript>();
                if (es)
                {
                    ColliderList.Add(collider.gameObject);
                    //ElementColor = mr.material.color;
                }
                else
                {
                    ElementScript parent_es = collider.transform.parent.gameObject.GetComponent <ElementScript>();
                    if (parent_es)
                    {
                        ColliderList.Add(parent_es.gameObject);
                    }
                }
            }
        }

        foreach (GameObject collider in ColliderList)
        {
            ElementScript es = collider.gameObject.GetComponent <ElementScript>();

            //es.SetPowerElement(PowerElement);

            float curElementValue = player.GetElementValue();

            es.SetPowerElement(SwordElement, Mathf.Min(curElementValue, player.GetShootSpend()));
            player.ReleaseElement(Mathf.Min(curElementValue, player.GetShootSpend()));
            AttackValue = 0;
        }
    }
Exemplo n.º 7
0
    /// <summary>
    ///  Method called by child objects to handle input behavior, will triggering property changes
    ///  based on current player state
    /// </summary>
    /// <param name="elementID"></param>
    public void OnHandleOnChildTouchUp(ElementScript element)
    {
        CmdServerSetVisibility(byte.Parse(element.name), myPaletteIndex);

        // Code to prevnt overwriting shapes of the same color
        //byte elementID = byte.Parse(element.name);
        //for (int index = 0; index < myMasterInstance.ServerElements.Count; index++)
        //{
        //    if (myMasterInstance.ServerElements[index].ID == elementID
        //     && myMasterInstance.ServerElements[index].shapePaletteIndex != myPaletteIndex)
        //    {
        //
        //    }
        //}
    }
Exemplo n.º 8
0
    public Link(string type, ElementScript firstElement, ElementScript secondElement)
    {
        this.type          = type;
        this.firstElement  = firstElement;
        this.secondElement = secondElement;

        Debug.Log("Creating link between " + firstElement.name + " and " + secondElement.name);

        dummy     = new GameObject();
        this.line = dummy.AddComponent <LineRenderer>();

        line.positionCount = 2;
        line.SetWidth(0.1f, 0.1f);

        joint = firstElement.gameObject.AddComponent <SpringJoint2D>();
        joint.connectedBody   = secondElement.rigidBody;
        joint.distance        = 0.4f;
        joint.frequency       = 2;
        joint.enableCollision = true;
    }
Exemplo n.º 9
0
    public void LoadLevel(Level level)
    {
        WorldManager.Instance.Init();

        makeWalls(level.size);

        foreach (var item in level.randomElements)
        {
            for (int i = 0; i < item.count; i++)
            {
                Vector3    position = new Vector3(Random.Range(-level.size, level.size), Random.Range(-level.size, level.size), 0);
                GameObject element  = Instantiate(elementPrefab, position, Quaternion.identity);
                element.name = item.type + " " + i.ToString();
                WorldManager.Instance.AddElement(element.GetComponent <ElementScript>());
            }
        }

        foreach (Level.Elements e in level.elements)
        {
            Vector3    position = new Vector3(e.x, e.y, 0);
            GameObject element  = Instantiate(elementPrefab, position, Quaternion.identity);
            element.name = e.id;
            ElementScript elementScript = element.GetComponent <ElementScript>();
            elementScript.Type = e.type;

            WorldManager.Instance.AddElement(elementScript);
        }

        foreach (Rule r in level.rules)
        {
            WorldManager.Instance.AddRule(r);
        }

        foreach (var l in level.links)
        {
            WorldManager.Instance.AddLink(l.id, l.first, l.second);
        }
    }
Exemplo n.º 10
0
    // Update is called once per frame
    void Update()
    {
        Collider[]      RodSensorColliders = Physics.OverlapSphere(RodSensor.transform.position, 2.0f);
        List <Collider> ColliderList       = new List <Collider>();

        foreach (Collider collider in RodSensorColliders)
        {
            if (collider.gameObject.tag == "Element" && !collider.isTrigger)
            //if (collider.gameObject.tag != "Ground" && !collider.isTrigger && collider.gameObject.tag != "Player")
            {
                MeshRenderer  mr = collider.gameObject.GetComponent <MeshRenderer>();
                ElementScript es = collider.gameObject.GetComponent <ElementScript>();
                if (es)
                {
                    {
                        //DrawLine(RodSensor.transform.position, hitPoint, ElementColor, ElementColor);
                        ColliderList.Add(collider);
                        //ElementColor = mr.material.color;
                    }
                }
                else
                {
                    ElementScript parent_es = collider.transform.parent.gameObject.GetComponent <ElementScript>();
                    if (parent_es)
                    {
                        ColliderList.Add(collider);
                    }
                }
            }
        }

        if (ColliderList.Count > 0)
        {
            float    minDis     = 100.0f;
            Collider closestObj = ColliderList[0];
            foreach (Collider collider in ColliderList)
            {
                Vector3 hitPoint = collider.ClosestPoint(RodSensor.transform.position);
                hitPoint.y = RodSensor.transform.position.y;
                float dis = Vector3.Distance(hitPoint, RodSensor.transform.position);
                if (dis < minDis)
                {
                    minDis     = dis;
                    closestObj = collider;
                }
            }

            MeshRenderer  mr = closestObj.gameObject.GetComponent <MeshRenderer>();
            ElementScript es = closestObj.gameObject.GetComponent <ElementScript>();
            if (es)
            {
                CanGetElement = true;
                RodElement    = es.GetElementType();
                ElementColor  = ElementDefine.GetElementColor(RodElement);

                Vector3 hitPoint = closestObj.ClosestPoint(RodSensor.transform.position);
                DrawLine(RodSensor.transform.position, hitPoint, ElementColor, ElementColor);
            }
            else
            {
                ElementScript parent_es = closestObj.transform.parent.gameObject.GetComponent <ElementScript>();
                if (parent_es)
                {
                    CanGetElement = true;
                    RodElement    = parent_es.GetElementType();
                    ElementColor  = ElementDefine.GetElementColor(RodElement);

                    Vector3 hitPoint = closestObj.ClosestPoint(RodSensor.transform.position);
                    hitPoint = closestObj.transform.position;
                    DrawLine(RodSensor.transform.position, hitPoint, ElementColor, ElementColor);
                }
            }
        }
        else
        {
            CanGetElement = false;
            RodElement    = ElementType.NONE;
            ElementColor  = ElementDefine.GetElementColor(RodElement);
            RemoveLine();
        }

        RodRenderer.material.color = ElementColor;
        //RodMaterial.color = ElementColor;
    }
Exemplo n.º 11
0
 public void AddElement(ElementScript elementScript)
 {
     elements.Add(elementScript.name, elementScript);
 }
Exemplo n.º 12
0
    private void UpdateLinks(LinkKey originalLinkKey)
    {
        Queue <string>   elementsToCheck   = new Queue <string>();
        HashSet <string> processedElements = new HashSet <string>();

        elementsToCheck.Enqueue(originalLinkKey.first);
        elementsToCheck.Enqueue(originalLinkKey.second);
        processedElements.Add(originalLinkKey.first);
        processedElements.Add(originalLinkKey.second);

        while (elementsToCheck.Count > 0)
        {
            string elementName = elementsToCheck.Dequeue();

            HashSet <LinkKey> neighborsOriginal;
            if (elementLinks.TryGetValue(elementName, out neighborsOriginal))
            {
                // see if there is a rule between the neighbor's type and the new element's type;
                ElementScript element = elements[elementName];

                HashSet <LinkKey> neighbors = new HashSet <LinkKey>(neighborsOriginal);

                foreach (LinkKey linkKey in neighbors)
                {
                    string otherElementName = linkKey.GetOther(elementName);

                    if (processedElements.Contains(otherElementName))
                    {
                        continue;
                    }

                    ElementScript otherElement = elements[otherElementName];

                    Debug.Log("Processing link between " + elementName + " and " + otherElementName);

                    Rule rule;

                    if (rules.TryGetValue(new RuleKey(element.Type, otherElement.Type), out rule))
                    {
                        // there is a rule between these two types
                        if (rule.hasLink)
                        {
                            addLink(rule.type, linkKey, element, otherElement);
                        }
                        else
                        {
                            // remove any link between these two objects
                            removeLink(linkKey, element, otherElement);
                        }

                        element.Type      = rule.firstTypeFinal;
                        otherElement.Type = rule.secondTypeFinal;
                        elementsToCheck.Enqueue(otherElementName);
                    }
                    else if (rules.TryGetValue(new RuleKey(otherElement.Type, element.Type), out rule))
                    {
                        // there is a rule between these two types
                        if (rule.hasLink)
                        {
                            addLink(rule.type, linkKey, otherElement, element);
                        }
                        else
                        {
                            // remove any link between these two objects
                            removeLink(linkKey, element, otherElement);
                        }

                        otherElement.Type = rule.firstTypeFinal;
                        element.Type      = rule.secondTypeFinal;
                        if (!processedElements.Contains(otherElementName))
                        {
                            elementsToCheck.Enqueue(otherElementName);
                        }
                    }
                    else
                    {
                        removeLink(linkKey, element, otherElement);
                    }
                }
            }

            processedElements.Add(elementName);
        }
    }
Exemplo n.º 13
0
 void selectItem(Transform item)
 {
     _selectedItem = item;
     _selectedItemScript = item.transform.GetComponent<ElementScript>();
     _selectedItemScript.select ();
 }
Exemplo n.º 14
0
 // Use this for initialization
 void Start()
 {
     elementScript = this.GetComponent <ElementScript>();
 }