void Start()
 {
     char_RigidBody = GetComponent <Kinematic>();
     goalComponent  = GetComponent <PathGoal>();
     arrive         = GetComponent <DynoArrive_PathFollow>();
     align          = GetComponent <DynoAlign_PathFollow>();
     seek           = GetComponent <DynoSeek_PathFollow>();
 }
 public UnitController(InputManager inputManager, InputState inputState, EntityWorld entityWorld, DisFieldMixer disFieldMixer, Lord lord)
 {
     this.inputManager = inputManager;
     this.inputState = inputState;
     this.entityWorld = entityWorld;
     this.disFieldMixer = disFieldMixer;
     this.lord = lord;
     pathGoal = null;
 }
        public void setPathGoal(PathGoal pathGoal)
        {
            this.pathGoal = pathGoal;
            pathGoal.updatePath();

            List<Entity> entities = pathGoal.getEntities();
            foreach (Entity entity in entities)
            {
                entity.GetComponent<component.Unit>().pathGoal = pathGoal;
            }
        }
        public void update()
        {
            MouseState mouseState;
            if (entitiesInSelection.Count > 0)
            {
                if (inputState.IsNewRightMouseClick(out mouseState))
                {
                    if (Global.Viewport.Contains(mouseState.Position))
                    {
                        Vector2 pos = Global.Camera.ScreenToWorld(mouseState.Position.ToVector2());
                        //Bag<Entity> entities = entityWorld.EntityManager.GetEntities(Aspect.All(typeof(component.Goal), typeof(component.Physics), typeof(component.Formation)));
                        int2 goalPos = new int2((int)pos.X / Global.tileSize, (int)pos.Y / Global.tileSize);
                        Bag<Entity> bag = new Bag<Entity>();
                        PathGoal pathGoal = new PathGoal(entitiesInSelection, disFieldMixer, new int2(Global.mapWidth, Global.mapHeight), goalPos);
                        setPathGoal(pathGoal);
                        pathGoal.updatePath();

                        EntityFormation formation = new EntityFormation(entitiesInSelection, disFieldMixer, goalPos);
                        foreach (Entity e in entitiesInSelection)
                        {
                            e.GetComponent<component.Formation>().EntityFormation = formation;
                        }
                        formation.update();
                    }
                }
            }

            if (Mouse.GetState().LeftButton == ButtonState.Pressed && !isSelecting)
            {
                isSelecting = true;
                firstCorner = Global.Camera.ScreenToWorld(Mouse.GetState().Position.ToVector2());

                foreach (Entity old in entitiesInSelection)
                {
                    old.GetComponent<component.HealthComponent>().Visible = false;
                }
                entitiesInSelection.Clear();
            }
            else if (Mouse.GetState().LeftButton == ButtonState.Released && isSelecting)
            {
                isSelecting = false;
                Vector2 secondCorner = Global.Camera.ScreenToWorld(Mouse.GetState().Position.ToVector2());
                Vector2 topLeft = new Vector2(Math.Min(firstCorner.X, secondCorner.X), Math.Min(firstCorner.Y, secondCorner.Y));
                Vector2 bottomRight = new Vector2(Math.Max(firstCorner.X, secondCorner.X), Math.Max(firstCorner.Y, secondCorner.Y));
                RectangleF rect = new RectangleF(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y);

                Console.WriteLine(rect.X / 32 + " - " + rect.Y / 32);

                if (rect.Width > 0 && rect.Height > 0)
                {
                    Bag<Entity> entities = entityWorld.EntityManager.GetEntities(Aspect.All(typeof(component.Physics), typeof(component.Formation), typeof(component.HealthComponent), typeof(component.Unit)));

                    foreach (Entity e in entities)
                    {
                        if (e.GetComponent<component.Unit>().Lord != lord)
                            continue;

                        component.Physics phys = e.GetComponent<component.Physics>();
                        Vector2 newPos = new Vector2(phys.Position.X * Global.tileSize, phys.Position.Y * Global.tileSize);
                        RectangleF entityRect = new RectangleF(newPos.X - 0.5f * 32f, newPos.Y - 0.5f * 32f, 1f * 32f, 1f * 32f);
                        if (rect.IntersectsWith(entityRect))
                        {
                            e.GetComponent<component.HealthComponent>().Visible = true;
                            entitiesInSelection.Add(e);
                        }
                    }

                    if (onSelectUnit != null)
                        onSelectUnit();
                }
            }

            if (isSelecting)
            {
                Vector2 secondCorner = Global.Camera.ScreenToWorld(Mouse.GetState().Position.ToVector2());
                Vector2 topLeft = new Vector2(Math.Min(firstCorner.X, secondCorner.X), Math.Min(firstCorner.Y, secondCorner.Y));
                Vector2 bottomRight = new Vector2(Math.Max(firstCorner.X, secondCorner.X), Math.Max(firstCorner.Y, secondCorner.Y));
                RectangleF rect = new RectangleF(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y);

                foreach (Entity e in tempEntitiesInSelection)
                {
                    e.GetComponent<component.HealthComponent>().Visible = false;
                }
                tempEntitiesInSelection.Clear();

                if (rect.Width > 0 && rect.Height > 0)
                {
                    Bag<Entity> entities = entityWorld.EntityManager.GetEntities(Aspect.All(typeof(component.Physics), typeof(component.Formation), typeof(component.HealthComponent)));
                    foreach (Entity e in entities)
                    {
                        component.Physics phys = e.GetComponent<component.Physics>();
                        Vector2 newPos = new Vector2(phys.Position.X * Global.tileSize, phys.Position.Y * Global.tileSize);
                        RectangleF entityRect = new RectangleF(newPos.X - 0.5f * 32f, newPos.Y - 0.5f * 32f, 1f * 32f, 1f * 32f);
                        if (rect.IntersectsWith(entityRect))
                        {
                            e.GetComponent<component.HealthComponent>().Visible = true;
                            tempEntitiesInSelection.Add(e);
                        }
                    }
                }
            }
        }
    public bool ValidPath(PathGoal other)
    {
        if (other.facingDirection == facingDirection)
        {
            return(false);
        }

        Vector3 direction = (other.transform.position - transform.position).normalized;

        if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
        {
            if (direction.x > 0)
            {
                if (other.facingDirection == Direction.North)
                {
                    return(false);
                }
                if (facingDirection == Direction.South)
                {
                    return(false);
                }
            }
            else
            {
                if (other.facingDirection == Direction.South)
                {
                    return(false);
                }
                if (facingDirection == Direction.North)
                {
                    return(false);
                }
            }
        }
        else
        {
            if (direction.y > 0)
            {
                if (other.facingDirection == Direction.East)
                {
                    return(false);
                }
                if (facingDirection == Direction.West)
                {
                    return(false);
                }
            }
            else
            {
                if (other.facingDirection == Direction.West)
                {
                    return(false);
                }
                if (facingDirection == Direction.East)
                {
                    return(false);
                }
            }
        }

        return(true);
    }
Beispiel #6
0
    // Use this for initialization

    void Start()
    {
        goal_script = GetComponent <PathGoal>();
        goal_script.setGoal(ordered_goals[goal_index]);
    }
Beispiel #7
0
 /***********************
  * Debug code!
  * // TODO: Remove debug code.
  * *********************/
 public void setPathGoal(PathGoal pathGoal)
 {
     this.pathGoal = pathGoal;
 }