Beispiel #1
0
    private void ClickedOnScreen()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector2      mousePos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
            RaycastHit2D hit      = Physics2D.Raycast(mousePos, Vector2.zero, playerRange);

            if (hit.collider != null)
            {
                GameObject currentHit = hit.collider.gameObject;
                Vector3    range      = currentHit.transform.position - playerPos;
                float      rangeID    = Mathf.Sqrt((range.x * range.x) + (range.y * range.y));

                if (rangeID <= playerRange)
                {
                    if (Data.GetHitpoints() > 0)
                    {
                        GameObject.FindGameObjectWithTag("Player").GetComponent <Animator>().SetTrigger("PlayerDig");
                        switch (currentHit.tag)
                        {
                        case "Pickups":
                            GoldInformation goldInformation = currentHit.gameObject.GetComponent <GoldInformation>();
                            Data.LoseHitpoints();
                            playerInformation.UpdateScore(goldInformation.IsHit(playerInformation.GetToolLevel(), playerInformation.GetToolStrength(), currentHit));
                            break;

                        case "Amethyst":
                            AmethystInformation amethystInformation = currentHit.gameObject.GetComponent <AmethystInformation>();
                            Data.LoseHitpoints();
                            playerInformation.UpdateScore(amethystInformation.IsHit(playerInformation.GetToolLevel(), playerInformation.GetToolStrength(), currentHit));
                            break;

                        case "Terrain":
                            TerrainInformation terrainInformation = currentHit.gameObject.GetComponent <TerrainInformation>();
                            Data.LoseHitpoints();
                            playerInformation.UpdateScore(0);
                            terrainInformation.IsHit(playerInformation.GetToolLevel(), playerInformation.GetToolStrength(), currentHit);
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        Data.Restart();
                    }
                }
            }
        }
    }
        public override void Update()
        {
            GameObject[] gobjs      = this.GuiController.GetGameObjectsOnMouse();
            GameObject   firstter   = gobjs.FirstOrDefault(go => go.gameObject.GetComponent <TerrainInformation>() != null);
            UnitEntity   attackUnit = null;

            if (firstter != null)
            {
                TerrainInformation terinfo = firstter.GetComponent <TerrainInformation>();
                TilePosition       tilepos = (TilePosition)terinfo.Terrain.Position;

                if (tilepos != null && (mousePoint != tilepos.Point && lastpos.Point != tilepos.Point) || refindMouse)
                {
                    refindMouse = false;
                    var            oldMousePath = mousePath;
                    TilePathFinder path         = new TilePathFinder((TileWorld)this.World);
                    mousePoint = tilepos.Point;

                    GuiViewHandler view = this.GuiController.GuiView;
                    Path <TileWorld, TilePosition> foundPath;

                    bool hasPath;
                    var  entities = this.World.GetEntities(tilepos).OfType <UnitEntity>();
                    attackUnit = entities.FirstOrDefault(ent => ent.Module <UnitInfoModule>().Controller != this.GuiController.GuiInfo.Player);

                    RemoveColorOfLastFound();
                    if (attackUnit == null)
                    {
                        hasPath             = path.FindFirst(lastpos, tilepos, out foundPath);
                        this.lastFoundEnemy = null;
                    }
                    else
                    {
                        Predicate <TilePosition> goalcond = pos =>
                        {
                            //var pointP = pos.Point;
                            //int difx = Math.Abs(pointP.X - mousePoint.X);
                            //int dify = Math.Abs(pointP.Y - mousePoint.Y);
                            //int attackrange = unitEntity.Module<AttackModule>().AttackRange;
                            //return attackrange >= difx && attackrange >= dify;
                            return(unitEntity.Module <AttackModule>().CanReachPoint(pos.Point, mousePoint));
                        };
                        hasPath             = path.FindFirst(lastpos, goalcond, pos => new Vector(pos.Point, mousePoint).Distance, out foundPath);
                        this.lastFoundEnemy = attackUnit;
                        var gobj = this.GuiController.Factory.GameObjectFromModel(attackUnit);
                        gobj.renderer.material.color = Color.red;
                    }

                    if (hasPath)
                    {
                        int movelength = unitEntity.Module <MoveModule>().MoveLength - this.pathLength();

                        mousePath = new Path <TileWorld, TilePosition>(foundPath.Map, foundPath.Road.Skip(1).Take(movelength));
                    }

                    view.drawRoute(mousePath, this.GuiController.GuiInfo.FocusColor);
                    if (oldMousePath.Map != null)
                    {
                        view.unDrawRoute(oldMousePath);
                    }
                }
            }

            if (Input.GetButtonDown("select_object") && !enemySelected)
            {
                if (mousePath.Map != null)
                {
                    fullroute.AddLast(mousePath);
                    if (mousePath.Road.Last != null)
                    {
                        this.lastpos = mousePath.Road.Last.Value;
                    }
                    this.GuiController.GuiView.unDrawRoute(mousePath);
                    mousePath = default(Path <TileWorld, TilePosition>);

                    QueueDeclareAction(this.lastFoundEnemy);
                    this.enemySelected = this.lastFoundEnemy != null;


                    //Finished = true;
                }
            }
            else if (Input.GetButtonDown("deselect_object"))
            {
                enemySelected = false;
                fullroute.RemoveLast();
                if (fullroute.Count == 0)
                {
                    lastpos = this.unitEntity.PositionAs <TilePosition>();
                }
                else
                {
                    lastpos = this.fullroute.Last.Value.Road.Last.Value;
                }
                QueueDeclareAction();
                this.refindMouse = true;
            }
            else if (Input.GetButtonDown("accept") || enemySelected || this.unitEntity.Module <MoveModule>().MoveLength == pathLength())
            {
                this.unit.renderer.material.color = originalColor;
                if (mousePath.Map != null)
                {
                    this.GuiController.GuiView.unDrawRoute(this.mousePath);
                }
                RemoveColorOfLastFound();
                Finished = true;
            }
        }