public PEnemy GenerateRandomEnemy() {
            if (st.EnemiesConfigs.IsNullOrEmpty()) {
                Debug.LogErrorFormat("Cannot crete unit cause {0} IsNullOrEmpty", nameof(st.EnemiesConfigs));
                return null;
            }

            var enemyConfig = PRandomUtil.Select(st.EnemiesConfigs);
            if (enemyConfig == null) {
                Debug.LogErrorFormat("Cannot crete unit cause null in {0}", nameof(st.EnemiesConfigs));
                return null;
            }

            var enemyUnit = enemyConfig.Prefab.CreateUnit();
            if (enemyUnit == null) {
                Debug.LogErrorFormat("Cannot crete unit from prefab");
                return null;
            }
            
            enemyUnit.HP = enemyConfig.MaxHP;
            enemyUnit.Vulnerability = enemyConfig.Vulnerability;
            enemyUnit.MovementSpeed = enemyConfig.MovementSpeed;
            enemyUnit.RotationSpeed = enemyConfig.RotationSpeed;
            enemyUnit.Damage = enemyConfig.Damage;

            float angle = PRandomUtil.Float(0, PMathUtil.TwoPI); 
            enemyUnit.transform.position = st.EnemyGenerationCenter + PMathUtil.PolarToDecart(st.EnemyGenerationRadius, angle);
            enemyUnit.transform.rotation = Quaternion.Euler(0f, 0f, -angle.AngleToDegree());

            return enemyUnit;
        }
        public List <GameObject> GenerateMapBox(Vector2 min, Vector2 max)
        {
            PMathUtil.AssureMinMax(ref min, ref max);

            var center = (min + max) * 0.5f;
            var diff   = max - min;

            var left = cnf.MapBox.CreateUnit();

            left.transform.position   = new Vector3(min.x - 0.5f, center.y);
            left.transform.localScale = new Vector3(1, diff.y, 1);

            var right = cnf.MapBox.CreateUnit();

            right.transform.position   = new Vector3(max.x + 0.5f, center.y);
            right.transform.localScale = new Vector3(1, diff.y, 1);

            var top = cnf.MapBox.CreateUnit();

            top.transform.position   = new Vector3(center.x, max.y + 0.5f);
            top.transform.localScale = new Vector3(diff.x, 1, 1);

            var bottom = cnf.MapBox.CreateUnit();

            bottom.transform.position   = new Vector3(center.x, min.y - 0.5f);
            bottom.transform.localScale = new Vector3(diff.x, 1, 1);

            return(new List <GameObject> {
                left, right, top, bottom
            });
        }
Exemple #3
0
        private void ProcessRotation(PBaseCharacter tank)
        {
            var positionDelta = tank.transform.position - st.Unit.transform.position;

            int rotationDirection = PMathUtil.CalcRotationRadDirection(
                st.Unit.Rotation2DRad(),
                Mathf.Atan2(positionDelta.y, positionDelta.x),
                1f.AngleToRad()
                );

            st.Unit.Rigidbody2D.angularVelocity = rotationDirection * st.Unit.RotationSpeed;
        }
        public GameObject Generate(Vector2 min, Vector2 max)
        {
            PMathUtil.AssureMinMax(ref min, ref max);

            var tile = cnf.Tile.CreateUnit();

            if (tile == null)
            {
                Debug.LogErrorFormat("Cannot generate tile from prefab");
                return(null);
            }

            tile.transform.position = (min + max) * 0.5f;
            tile.Size = (max - min);

            return(tile.gameObject);
        }
Exemple #5
0
        private void ProcessMovement()
        {
            bool forward  = Input.GetKey(KeyCode.W);
            bool backward = Input.GetKey(KeyCode.S);

            if (forward == backward)
            {
                st.Tank.Rigidbody2D.velocity = Vector2.zero;
            }
            else if (forward)
            {
                st.Tank.Rigidbody2D.velocity = PMathUtil.PolarToDecart(st.Tank.MovementSpeed, st.Tank.Rotation2DRad());
            }
            else
            {
                st.Tank.Rigidbody2D.velocity = -PMathUtil.PolarToDecart(st.Tank.MovementSpeed, st.Tank.Rotation2DRad());
            }
        }
Exemple #6
0
        private void ProcessForward(PBaseCharacter tank)
        {
            var     movementDeltaPerSec = PMathUtil.PolarToDecart(st.Unit.MovementSpeed, st.Unit.Rotation2DRad());
            Vector3 movementDelta       = movementDeltaPerSec * Time.fixedDeltaTime;

            var positionDelta = st.Unit.transform.position - tank.transform.position;

            float dist           = positionDelta.sqrMagnitude;
            float distIfForward  = (positionDelta + movementDelta).sqrMagnitude;
            float distIfBackward = (positionDelta - movementDelta).sqrMagnitude;

            if (dist < distIfForward && dist < distIfBackward)
            {
                st.Unit.Rigidbody2D.velocity = Vector2.zero;
            }
            else
            {
                st.Unit.Rigidbody2D.velocity = distIfForward < distIfBackward ? movementDeltaPerSec : -movementDeltaPerSec;
            }
        }