Esempio n. 1
0
 public static void Spawn(int x, int y, GameObject prefab)
 {
     World.Grid grid = World.Instance.GetGrid();
     World.Cell cell = grid.GetCell(x, y);
     if (!cell.IsBlocked)
     {
         //Vector3 position = new Vector3(x, heightOffset, y);
         GameObject spawn = Instantiate <GameObject>(prefab);
         spawn.transform.position = spawn.transform.position;
         SynchronizedActor actor = spawn.GetComponent <SynchronizedActor>();
         if (actor != null)
         {
             Entity entity = null;
             if (actor.tag == "Player")
             {
                 entity = new Player(x, y, EntityStatistics.GetRandomPlayerStats());
             }
             else if (actor.tag == "Enemy")
             {
                 MonsterConfig config = spawn.GetComponent <MonsterConfig>();
                 entity = new Monster(x, y, config.GetStats());
             }
             if (entity != null)
             {
                 actor.InitActor(entity);
             }
         }
     }
 }
Esempio n. 2
0
 public MoveResult(ResultValue result, World.Cell cell, int x, int y)
 {
     m_result = result;
     m_cell   = cell;
     m_x      = x;
     m_y      = y;
 }
Esempio n. 3
0
    public AttackResult Attack(int x, int y)
    {
        AttackResult result = null;

        World.Cell cell     = World.Instance.GetGrid().GetCell(x, y);
        int        distance = (int)(new Vector2((float)x, (float)y) - m_actor.Entity.GetPositionVector()).magnitude;

        if (!cell.ContainsEntity)
        {
            Debug.Log("No Target");
            return(new AttackResult(AttackResult.ResultValue.NoTarget, null));
        }


        foreach (Weapon wep in m_weapons)
        {
            if (wep == null)
            {
                Debug.Log("Null weapon");
                break;
            }
            Debug.Log("Using weapon: " + wep.Name);
            if (wep.EnergyCost <= m_actor.Stats.Energy)             //has energy
            {
                Debug.Log("Has Energy");
                if (distance > wep.Range)                           //within range
                {
                    Debug.Log("Out of range");
                    result = new AttackResult(AttackResult.ResultValue.OutOfRange, wep);
                }
                else
                {
                    Debug.Log("Within Range");
                    if (wep.EnergyCost > 0)
                    {
                        m_actor.Stats.Energy -= wep.EnergyCost;
                    }
                    if (wep.Hit(m_actor.Stats.Attack))              //ghit
                    {
                        Debug.Log("Hit");
                        result = new AttackResult(AttackResult.ResultValue.Hit, wep);
                    }
                    else
                    {
                        Debug.Log("Miss");
                        result = new AttackResult(AttackResult.ResultValue.Miss, wep);
                    }
                    break;
                }
            }
            else
            {
                Debug.Log("Out of Energy");
                result = new AttackResult(AttackResult.ResultValue.NoEnergy, wep);
            }
        }

        return(result);
    }
Esempio n. 4
0
 public virtual void Move(int x, int y)
 {
     //Debug.Log("Entity " + _id + " Move to " + x + ", " + y);
     _x = x;
     _y = y;
     if (_pos != null)
     {
         _pos.SetEntity(null);
     }
     _pos = World.GetInstance().GetGrid().GetCell(x, y);
     _pos.SetEntity(this);
 }
Esempio n. 5
0
 private void Generate()
 {
     for (var i = 0; i < map.GetLength(0); i++)
     {
         for (var j = 0; j < map.GetLength(1); j++)
         {
             var cell = new World.Cell {X = i, Y = j};
             var tilePosition = new Vector3(cell.X*World.CellSize.x, 0f, cell.Y*World.CellSize.y) + offset;
             var tile = GetPopulatedTile(cell, tilePosition);
             tile.transform.name = string.Format("Tile_{0}_{1}", i, j);
         }
     }
 }
Esempio n. 6
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="direction">as map direction not unity direction</param>
    public MoveResult Move(Vector2 direction)
    {
        //float move = m_remainingMove + moveSpeed;
        //m_remainingMove = move - (Mathf.Abs(move));

        //Debug.Log("Position on Map: " + MapPosition +", Position on World: "+transform.position);
        //Debug.Log("Move Input Direction: " + direction);

        float absX = Mathf.Abs(direction.x);
        float absY = Mathf.Abs(direction.y);
        float rand = UnityEngine.Random.value;
        int   x, y;

        if (absX > absY || (absX == absY && rand < 0.5f))
        {
            x = Math.Sign(direction.x);
            y = 0;
        }
        else
        {
            x = 0;
            y = Math.Sign(direction.y);
        }

        MoveResult result;
        Vector2    targetPosition = MoveGoal + new Vector2(x, y);

        x = (int)targetPosition.x;
        y = (int)targetPosition.y;
        World.Cell cell           = World.Instance.GetGrid().GetCell(x, y);
        //Debug.Log("Cell at " + targetPosition+":  " + cell.ToString());
        if (cell.IsBlocked)
        {
            //Debug.Log("Cell Blocked");
            result = new MoveResult(MoveResult.ResultValue.TileBlocked, cell, x, y);
        }
        else if (cell.ContainsEntity)
        {
            //Debug.Log("Cell Occupied");
            result = new MoveResult(MoveResult.ResultValue.TileOccupied, cell, x, y);
        }
        else
        {
            //Debug.Log("Move OK");
            result   = new MoveResult(MoveResult.ResultValue.Ok, cell, x, y);
            MoveGoal = targetPosition;
            m_syncActor.AssignAction(new SyncMoveAction(this, targetPosition));
        }
        return(result);
    }
    private World.Cell GetMouseOverCell()
    {
        RaycastHit hit;
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            int     x   = (int)(hit.point.x + 0.5);
            int     y   = (int)(hit.point.y + 0.5);
            int     z   = (int)(hit.point.z + 0.5);
            Vector3 loc = new Vector3(x, y, z);
            //Debug.Log(loc);
            World.Cell cell = World.Instance.GetGrid().GetCell((int)loc.x, (int)loc.z);
            return(cell);
        }
        return(null);
    }
    /// <summary>
    /// Checks if theres anyone nearby within melee range and attacks. Return true if attack was made.
    /// Ranged attack not supported
    /// </summary>
    /// <returns></returns>
    private bool doAttack()
    {
        World.Grid grid = World.Instance.GetGrid();
        int        x, y;

        m_actor.Entity.GetPosition(out x, out y);
        World.Cell               myCell = grid.GetCell(x, y);
        World.Cell[]             neighbours = grid.GetNeighbours4(myCell);
        List <SynchronizedActor> pTargets = new List <SynchronizedActor>();

        foreach (World.Cell cell in neighbours)
        {
            if (cell.ContainsEntity)
            {
                Entity e = cell.GetEntity();
                if (e.GetType() == typeof(Player))
                {
                    pTargets.Add(e.Actor);
                    break;
                }
                else if (UnityEngine.Random.value < Constants.FRIENDLY_FIRE_CHANCE)
                {
                    pTargets.Add(e.Actor);
                }
            }
        }
        if (pTargets.Count > 0)
        {
            SynchronizedActor            target = pTargets[UnityEngine.Random.Range(0, pTargets.Count)];
            AttackingEntity.AttackResult result = CombatSolver.Fight(m_actor, target);
            if (result.Result == AttackingEntity.AttackResult.ResultValue.Hit || result.Result == AttackingEntity.AttackResult.ResultValue.Miss)
            {
                Synchronizer.Continue(m_actor, result.Weapon.TimeCost);
            }
            return(true);
        }
        return(false);
    }
    // Update is called once per frame
    void Update()
    {
        if (m_myTurn)
        {
            World.Cell mouseOverCell = GetMouseOverCell();
            if (mouseOverCell != null && mouseOverCell.ContainsEntity)
            {
                ApplyLOS();
                int    x, y;
                Entity ent = mouseOverCell.GetEntity();
                ent.GetPosition(out x, out y);

                if (!"Player".Equals(ent.GetType().ToString()) && ent.GetCell().IsVisible())
                {
                    SetCrosshair(x, y);
                }
                else
                {
                    HideCrosshair();
                }

                if (Input.GetMouseButtonDown(0))
                {
                    AttackingEntity.AttackResult res = CombatSolver.Fight(m_actor, mouseOverCell.GetEntity().Actor);

                    if (res != null && res.Weapon != null)
                    {
                        Synchronizer.Continue(m_actor, res.Weapon.TimeCost);
                        return;
                    }
                    else if (res.Result == AttackingEntity.AttackResult.ResultValue.NoEnergy)
                    {
                        Debug.Log(res.Weapon.Name + " Out of energy");
                    }
                }
            }
            else
            {
                HideCrosshair();
            }

            Vector2 move = Vector2.zero;
            if (Input.GetButtonDown("Horizontal"))
            {
                float h = Input.GetAxis("Horizontal");
                move.x = Mathf.Sign(h);
            }
            if (Input.GetButtonDown("Vertical"))
            {
                float h = Input.GetAxis("Vertical");

                move.y = Mathf.Sign(h);
            }

            if (move != Vector2.zero)
            {
                //Debug.Log("Move: " + move);
                MovingEntity.MoveResult result = m_movingEntity.Move(move);
                switch (result.Result)
                {
                case MovingEntity.MoveResult.ResultValue.Ok:
                    Synchronizer.Continue(m_actor, m_movingEntity.moveActionCost);

                    if (m_levelBuilder.LevelType != LevelType.Ambush)
                    {
                        if (m_levelBuilder.EndPoint.X == result.Cell.X && m_levelBuilder.EndPoint.Y == result.Cell.Y)
                        {
                            if (OnPlayerMovedToEndEvent != null)
                            {
                                OnPlayerMovedToEndEvent();
                            }
                        }

                        if (m_levelBuilder.ObjectivePoint.X == result.Cell.X && m_levelBuilder.ObjectivePoint.Y == result.Cell.Y)
                        {
                            if (OnPlayerMovedToObjectiveEvent != null)
                            {
                                OnPlayerMovedToObjectiveEvent();
                            }
                        }
                    }
                    break;

                case MovingEntity.MoveResult.ResultValue.TileBlocked:
                    //Debug.Log("Tile blocked!");
                    break;

                case MovingEntity.MoveResult.ResultValue.TileOccupied:
                    AttackingEntity.AttackResult res = CombatSolver.Fight(m_actor, result.Cell.GetEntity().Actor);
                    Synchronizer.Continue(m_actor, res.Weapon.TimeCost);
                    break;
                }
            }
        }
    }
Esempio n. 10
0
    private void ApplyLOS()
    {
        World.Cell[,] grid = World.Instance.GetGrid().GetCells();

        int x, y;

        Synchronizer.Instance.Player.Entity.GetPosition(out x, out y);

        List <SynchronizedActor> actors = Synchronizer.Instance.GetActors();
        bool skipfirst = true;

        foreach (SynchronizedActor a in actors)
        {
            if (skipfirst)
            {
                skipfirst = false;
            }
            else
            {
                int xe, ye;
                a.Entity.GetPosition(out xe, out ye);

                float xd = xe - x;
                float yd = ye - y;

                bool visible = true;
                if (Math.Abs(xd) < Math.Abs(yd))
                {
                    float d = 0.0f;
                    for (int i = 0; i < Math.Abs(yd); i++)
                    {
                        int xc = (xd < 0 ? -i : i) + x;
                        d += xd / yd;
                        int yc = (int)d + y;

                        World.Cell c = grid[xc, yc];
                        if (c.IsBlocked)
                        {
                            visible = false;
                            break;
                        }
                    }
                }
                else
                {
                    float d = 0.0f;
                    for (int i = 0; i < Math.Abs(xd); i++)
                    {
                        int yc = (yd < 0 ? -i : i) + y;
                        d += yd / xd;
                        int xc = (int)d + x;

                        World.Cell c = grid[xc, yc];
                        if (c.IsBlocked)
                        {
                            visible = false;
                            break;
                        }
                    }
                }
                grid[xe, ye].SetVisible(visible);
            }
        }
    }
Esempio n. 11
0
    private World.Cell GetWrappedCell(int x, int y)
    {
        var mapHorizontalCount = map.GetLength(0);
        var mapVerticalCount = map.GetLength(1);

        var cell = new World.Cell { X = x % mapHorizontalCount, Y = y % mapVerticalCount };

        if (cell.X < 0)
            cell.X += mapHorizontalCount;
        if (cell.X >= mapHorizontalCount)
            cell.X -= mapHorizontalCount;

        if (cell.Y < 0)
            cell.Y += mapVerticalCount;
        if (cell.Y >= mapVerticalCount)
            cell.Y -= mapVerticalCount;

        return cell;
    }
Esempio n. 12
0
    private void Movement(Vector2 move, float time)
    {
        var maxSpeed = Input.GetButton("WalkMode")
            ? WalkSpeed
            : RunSpeed;

        speed = move.SqrMagnitude() > 0.1f
            ? Mathf.Clamp(speed + Acceleration * time, StartSpeed, maxSpeed)
            : Mathf.Clamp(speed - Acceleration * time, 0f, maxSpeed);

        var gravityDisplacement = Physics.gravity * time;
        playerController.Move(gravityDisplacement);

        if (UseShift)
        {
            var curCell = World.GetCell(transform.position);
            var cellDelta = curCell - lastCell;

            if (cellDelta.SqrMagnitude() > 0.1f)
            {
                Debug.Log("CELL SHIFT: " + cellDelta);
                var shift = new Vector3(World.CellSize.x * cellDelta.X, 0f, World.CellSize.y * cellDelta.Y);
                Debug.Log("SHIFT: " + shift);

                //StartCoroutine(World.ShiftRoutine(shift));
                World.Shift(shift);
                curCell = World.GetCell(transform.position);
            }

            lastCell = curCell;
        }
    }