示例#1
0
        /// <summary>
        /// Creates a tank instance and adds it to specified city
        /// </summary>
        void DropTankOnCity()
        {
            // Get a random big city
            int cityIndex = map.GetCityIndex("Paris", "France");

            // Get city location
            Vector2 cityPosition = map.cities [cityIndex].unity2DLocation;

            if (tank != null)
            {
                DestroyImmediate(tank.gameObject);
            }
            tank = DropTankOnPosition(cityPosition);

            // Zoom into tank
            map.FlyToLocation(cityPosition, 2.0f, 0.15f);

            // Enable move on click in this demo
            enableAddTowerOnClick = false;
            enableClickToMoveShip = false;
            enableClickToMoveTank = true;

            // Finally, signal me when tank starts and stops
            tank.OnMoveStart     += (thisTank) => Debug.Log("Tank has starting moving to " + thisTank.destination + " location.");
            tank.OnMoveEnd       += (thisTank) => Debug.Log("Tank has stopped at " + thisTank.currentMap2DLocation + " location.");
            tank.OnCountryEnter  += (thisTank) => Debug.Log("Tank has entered country " + map.GetCountry(thisTank.currentMap2DLocation).name + ".");
            tank.OnProvinceEnter += (thisTank) => Debug.Log("Tank has entered province " + map.GetProvince(thisTank.currentMap2DLocation).name + ".");
        }
示例#2
0
        // Executes on each frame - move ships and tanks around
        void Update()
        {
            Vector2 destination;

            if (units != null)
            {
                // Make units move around the board
                unitIndex++;
                if (unitIndex >= units.Count)
                {
                    unitIndex = 0;
                }
                GameObjectAnimator unit = units [unitIndex];
                if (!unit.isMoving)
                {
                    if (unit.type == (int)UNIT_TYPE.TANK)
                    {
                        destination = GetRandomCityPosition();
                    }
                    else                                // it's a ship
                    {
                        destination = GetRandomWaterPosition();
                    }
                    unit.MoveTo(destination, 0.1f);
                }
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                ship.visible = !ship.visible;
            }
        }
        /// <summary>
        /// Smoothly moves this game object to given map destination along route of points.
        /// </summary>
        /// <param name="durationType">Step: each step will take the same duration, Route: the given duration is for the entire route, MapLap: the duration is the time to cross entire map. Default duration type is 'Step'. Use 'MapLap' if you pass a custom set of non-continuous points to ensure a consistent speed of movement.</param>
        public static GameObjectAnimator WMSK_MoveTo(this GameObject o, List <Vector2> route, float duration, DURATION_TYPE durationType = DURATION_TYPE.Step)
        {
            GameObjectAnimator anim = o.GetComponent <GameObjectAnimator>() ?? o.AddComponent <GameObjectAnimator>();

            anim.MoveTo(route, duration, durationType);
            return(anim);
        }
        void Start()
        {
            // Get a reference to the World Map API:
            map = WMSK.instance;

            // Get a reference to the viewport gameobject (we'll enable/disable it when changing modes)
            viewport = GameObject.Find("Viewport");

            // Get the material of the fade plane
            fadePlane    = Camera.main.transform.Find("FadePlane").gameObject;
            fadeMaterial = fadePlane.GetComponent <Renderer>().sharedMaterial;

            // UI Setup - non-important, only for this demo
            buttonStyle                   = new GUIStyle();
            buttonStyle.alignment         = TextAnchor.MiddleCenter;
            buttonStyle.normal.background = Texture2D.whiteTexture;
            buttonStyle.normal.textColor  = Color.white;

            // setup GUI resizer - only for the demo
            GUIResizer.Init(800, 500);

            map.CenterMap();

            // Create tank
            kathmanduLocation = map.GetCity("Kathmandu", "Nepal").unity2DLocation;
            beijingLocation   = map.GetCity("Beijing", "China").unity2DLocation;
            tank = DropTankOnPosition(kathmanduLocation);

            // Start movement
            tank.MoveTo(beijingLocation, 0.1f);
            tank.OnMoveEnd += (anim) => SwitchDestination();
        }
示例#5
0
        IEnumerator FireThis(float delay, GameObjectAnimator bulletAnim, Vector3 startAnchor, Vector2 destination, float bulletSpeed, float arcMultiplier = 1f, bool testAnchor = false)
        {
            yield return(new WaitForSeconds(delay));

            Renderer renderer = bulletAnim.gameObject.GetComponent <Renderer> ();

            if (renderer != null)
            {
                renderer.enabled = true;
            }
            bulletAnim.heightMode        = HEIGHT_OFFSET_MODE.ABSOLUTE_CLAMPED;
            bulletAnim.pivotY            = 0f;
            bulletAnim.autoRotation      = false;
            bulletAnim.terrainCapability = TERRAIN_CAPABILITY.Any;
            bulletAnim.autoScale         = autoScale;
            bulletAnim.arcMultiplier     = arcMultiplier;
            Vector3 worldPos = transform.TransformPoint(startAnchor);

            if (testAnchor)
            {
                bulletAnim.transform.position = worldPos;
                yield break;
            }
            Vector2 startingPosition = map.WorldToMap2DPosition(worldPos);

            bulletAnim.startingMap2DLocation = startingPosition;
            float h = map.WorldToAltitude(worldPos);

            bulletAnim.altitudeStart = h;
            bulletAnim.altitudeEnd   = 0;
            float duration = Vector2.Distance(destination, startingPosition) * 200f / bulletSpeed;

            bulletAnim.MoveTo(destination, duration);
        }
示例#6
0
        /// <summary>
        /// Returns the registered Game Object with a given unique identifier
        /// </summary>
        public GameObjectAnimator VGOGet(int uniqueId)
        {
            GameObjectAnimator o = null;

            vgos.TryGetValue(uniqueId, out o);
            return(o);
        }
        void ColorTankMouseUp(GameObjectAnimator obj)
        {
            // Changes tank color to white
            Renderer renderer = obj.GetComponentInChildren <Renderer>();

            renderer.sharedMaterial.color = Color.yellow;
        }
示例#8
0
        void MapClickHandler(float x, float y, int buttonIndex)
        {
            Vector2 targetPosition = new Vector2(x, y);

            if (buttonIndex == 0)               // left click invoke look at
            {
                tank1.WMSK_LookAt(targetPosition);
            }
            else if (buttonIndex == 1)                 // right click fires

            // Create bullet
            {
                GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                bullet.GetComponent <Renderer>().material.color = Color.yellow;
                bullet.transform.localScale = Misc.Vector3one * 0.5f;

                // Animate bullet!
                Vector3            tankCannonAnchor = new Vector3(0f, 1.55f, 0.85f);            // this is the position relative to the tank pivot (note that the tank pivot is at bottom of tank per model definition)
                float              bulletSpeed      = 10.0f;
                float              bulletArc        = 1.1f;
                GameObjectAnimator bulletAnim       = tank1.WMSK_Fire(bullet, tankCannonAnchor, targetPosition, bulletSpeed, bulletArc);

                // We use the OnMoveEnd event of the bullet to destroy it once it reaches its destination
                bulletAnim.OnMoveStart += BulletFired;
                bulletAnim.OnMoveEnd   += BulletImpact;
            }
        }
        void ColorTankHover(GameObjectAnimator obj)
        {
            // Changes tank color - but first we store original color inside its attribute bag
            Renderer renderer = obj.GetComponentInChildren <Renderer>();

            obj.attrib["color"]     = renderer.sharedMaterial.color;
            renderer.material.color = Color.yellow;             // notice how I use material and not sharedmaterial - this is to prevent affecting all clone instances - we just want to color this one, so we need to make this material unique.
        }
示例#10
0
 /// <summary>
 /// Returns true if the game object is already registered in the viewport collection.
 /// </summary>
 /// <returns><c>true</c>, if viewport is registered was rendered, <c>false</c> otherwise.</returns>
 internal bool VGOIsRegistered(GameObjectAnimator o)
 {
     if (vgos == null)
     {
         return(false);
     }
     return(vgos.ContainsKey(o.uniqueId));
 }
        void RestoreTankColor(GameObjectAnimator obj)
        {
            // Restores original tank color
            Renderer renderer  = obj.GetComponentInChildren <Renderer>();
            Color    tankColor = obj.attrib["color"];           // get back the original color from attribute bag

            renderer.sharedMaterial.color = tankColor;
        }
        // Create tank instance and add it to the map
        GameObjectAnimator DropTankOnPosition(Vector2 mapPosition)
        {
            GameObject         tankGO = Instantiate(Resources.Load <GameObject> ("Tank/CompleteTank"));
            GameObjectAnimator tank   = tankGO.WMSK_MoveTo(mapPosition);

            tank.autoRotation = true;
            return(tank);
        }
示例#13
0
        /// <summary>
        /// Creates a tower instance and adds it to given coordinates
        /// </summary>
        void AddTowerAtPosition(float x, float y)
        {
            // Instantiate game object and position it instantly over the city
            GameObject         tower = Instantiate(towerPrefab);
            GameObjectAnimator anim  = tower.WMSK_MoveTo(x, y);

            anim.autoScale = false;
        }
示例#14
0
        /// <summary>
        /// This function adds a standard sphere primitive to the map. The difference here is that the pivot of the sphere is centered in the sphere. So we make use of pivotY property to specify it and
        /// this way the positioning over the terrain will work. Otherwise, the sphere will be cut by the terrain (the center of the sphere will be on the ground - and we want the sphere on top of the terrain).
        /// </summary>
        void AddSphere()
        {
            GameObject         sphere   = Instantiate(spherePrefab);
            Vector2            position = map.GetCity("Lhasa", "China").unity2DLocation;
            GameObjectAnimator anim     = sphere.WMSK_MoveTo(position);

            anim.pivotY = 0.5f;
            map.FlyToLocation(position, 2f, 0.1f);
        }
        // Create tank instance and add it to the map
        GameObjectAnimator DropTankOnPosition(Vector2 mapPosition)
        {
            GameObject         tankGO = Instantiate(Resources.Load <GameObject> ("Tank/CompleteTank"));
            GameObjectAnimator tank   = tankGO.WMSK_MoveTo(mapPosition);

            tank.autoRotation     = true;
            tank.attrib ["Color"] = tank.GetComponentInChildren <Renderer> ().sharedMaterial.color;
            return(tank);
        }
示例#16
0
 void UpdateViewportObjectsVisibility()
 {
     // Update animators
     CheckVGOsArrayDirty();
     for (int k = 0; k < vgosCount; k++)
     {
         GameObjectAnimator vgo = vgos[k];
         vgo.UpdateTransformAndVisibility();
     }
 }
        public static void WMSK_LookAt(this GameObject o, Vector2 destination)
        {
            GameObjectAnimator anim = o.GetComponent <GameObjectAnimator>();

            if (anim == null)
            {
                return;
            }
            anim.LookAt(destination);
        }
        /// <summary>
        /// Creates a new ship on position.
        /// </summary>
        GameObjectAnimator DropShipOnPosition(Vector2 position)
        {
            // Create ship
            GameObject shipGO = Instantiate(Resources.Load <GameObject> ("Ship/VikingShip"));

            ship = shipGO.WMSK_MoveTo(position);
            ship.terrainCapability = TERRAIN_CAPABILITY.OnlyWater;
            ship.autoRotation      = true;
            return(ship);
        }
示例#19
0
        // Create tank instance and add it to the map
        GameObjectAnimator DropTankOnPosition(Vector2 mapPosition)
        {
            GameObject tankGO = Instantiate(Resources.Load <GameObject> ("Tank/CompleteTank"));

            tankGO.transform.localScale = Misc.Vector3one * 0.25f;
            tank = tankGO.WMSK_MoveTo(mapPosition);
            tank.autoRotation      = true;
            tank.terrainCapability = TERRAIN_CAPABILITY.OnlyGround;
            tank.maxSearchCost     = 10;
            return(tank);
        }
示例#20
0
 /// <summary>
 /// Unregisters the game object in the viewport collection.
 /// </summary>
 public void VGOUnRegisterGameObject(GameObjectAnimator o)
 {
     if (vgos == null || o == null || o.uniqueId == 0)
     {
         return;
     }
     if (vgos.ContainsKey(o.uniqueId))
     {
         vgos.Remove(o.uniqueId);
     }
 }
示例#21
0
        // Create tank instance and add it to the map
        GameObjectAnimator DropTankOnPosition(Vector2 mapPosition)
        {
            GameObject tankGO = Instantiate(tankPrefab);

            tankGO.transform.localScale = Misc.Vector3one * 0.25f;
            tank                   = tankGO.WMSK_MoveTo(mapPosition);
            tank.type              = (int)UNIT_TYPE.TANK;
            tank.autoRotation      = true;
            tank.terrainCapability = TERRAIN_CAPABILITY.OnlyGround;
            return(tank);
        }
示例#22
0
        void LaunchShip()
        {
            int     cityIndex     = 0;
            Vector2 cityPosition  = map.cities[cityIndex].unity2DLocation;
            Vector2 waterPosition = cityPosition;

            // Create ship
            ship = DropShipOnPosition(waterPosition);
            // Fly to the location of ship with provided zoom level
            map.FlyToLocation(waterPosition, 2.0f, 0.1f);
        }
示例#23
0
        /// <summary>
        /// Fires a bullet, cannon-ball, missile, etc.
        /// </summary>
        /// <param name="delay">Firing delay. Gives time to the unit so it can orient to target.</param>
        /// <param name="bullet">Bullet. You must supply your own bullet gameobject.</param>
        /// <param name="startAnchor">Start anchor. Where does the bullet appear? This vector3 is expressed in local coordinates of the firing unit and ignores its scale.</param>
        /// <param name="destination">Destination. Target 2d map coordinates.</param>
        /// <param name="duration">Duration for the bullet to reach its destination..</param>
        /// <param name="arcMultiplier">Pass a value greater than 1 to produce a parabole.</param>
        public GameObjectAnimator Fire(float delay, GameObject bullet, Vector3 startAnchor, Vector2 destination, float bulletSpeed, float arcMultiplier = 1f, bool testAnchor = false)
        {
            GameObjectAnimator bulletAnim = bullet.GetComponent <GameObjectAnimator> () ?? bullet.AddComponent <GameObjectAnimator> ();
            Renderer           renderer   = bullet.GetComponent <Renderer> ();

            if (renderer != null)
            {
                renderer.enabled = false;
            }
            StartCoroutine(FireThis(delay, bulletAnim, startAnchor, destination, bulletSpeed, arcMultiplier, testAnchor));
            return(bulletAnim);
        }
示例#24
0
 void UpdateViewportObjectsLoop()
 {
     // Update animators
     CheckVGOsArrayDirty();
     for (int k = 0; k < vgosCount; k++)
     {
         GameObjectAnimator vgo = vgos[k];
         if (vgo.isMoving || vgo.mouseIsOver || (vgo.lastKnownPosIsOnWater && vgo.enableBuoyancyEffect))
         {
             vgo.PerformUpdateLoop();
         }
     }
 }
        // Create tank instance and add it to the map
        GameObjectAnimator DropTankOnPosition(Vector2 mapPosition)
        {
            GameObject tankGO = Instantiate(Resources.Load <GameObject>("Tank/CompleteTank"));

            tankGO.transform.localScale = Vector3.one * 0.25f;             // make tank smaller

            // Add tank to the viewport
            GameObjectAnimator tank = tankGO.WMSK_MoveTo(mapPosition);

            tank.autoRotation      = true;
            tank.terrainCapability = TERRAIN_CAPABILITY.OnlyGround;
            return(tank);
        }
示例#26
0
        /// <summary>
        /// Get a list of registered Game Objects inside a rectangle.
        /// </summary>
        /// <returns>The get.</returns>
        /// <param name="rect">Rect.</param>
        /// <param name="predicate">Predicate.</param>
        public List <GameObjectAnimator> VGOGet(Rect rect)
        {
            List <GameObjectAnimator> selected = new List <GameObjectAnimator>();

            for (int k = 0; k < vgosCount; k++)
            {
                GameObjectAnimator go = vgos[k];
                if (rect.Contains(go.currentMap2DLocation))
                {
                    selected.Add(go);
                }
            }
            return(selected);
        }
示例#27
0
 /// <summary>
 /// Get a list of registered Game Objects inside a rectangle. Optionally provide a predicate function to filer units.
 /// </summary>
 /// <returns>The get.</returns>
 /// <param name="rect">Rect.</param>
 /// <param name="predicate">Predicate function that returns true for each unit passed.</param>
 public int VGOGet(Rect rect, List <GameObjectAnimator> results, AttribPredicate predicate = null)
 {
     CheckVGOsArrayDirty();
     results.Clear();
     for (int k = 0; k < vgosCount; k++)
     {
         GameObjectAnimator go = vgos[k];
         if (rect.Contains(go.currentMap2DLocation) && (predicate == null || predicate(go.attrib)))
         {
             results.Add(go);
         }
     }
     return(results.Count);
 }
        /// <summary>
        /// Fires a bullet, cannon-ball, missile, etc.
        /// </summary>
        /// <param name="bullet">Bullet. You must supply your own bullet gameobject.</param>
        /// <param name="startAnchor">Start anchor. Where does the bullet appear? This vector3 is expressed in local coordinates of the firing unit and ignores its scale.</param>
        /// <param name="destination">Destination. Target 2d map coordinates.</param>
        /// <param name="duration">Duration for the bullet to reach its destination..</param>
        /// <param name="arcMultiplier">Pass a value greater than 1 to produce a parabole.</param>
        /// <param name="delay">Firing delay. Gives time to the unit so it can orient to target.</param>
        /// <param name="orientToTarget">Orient to target before firing.</param>
        /// <param name="testAnchor">Drops the bullet at the start of the trajectory. Useful to check the anchor is correct.</param>
        public static GameObjectAnimator WMSK_Fire(this GameObject o, GameObject bullet, Vector3 startAnchor, Vector2 destination, float bulletSpeed, float arcHeight = 1f, float delay = 1f, bool orientToTarget = true, bool testAnchor = false)
        {
            GameObjectAnimator goAnim = o.GetComponent <GameObjectAnimator>();

            if (goAnim == null)
            {
                return(null);
            }
            if (orientToTarget)
            {
                goAnim.LookAt(destination);
            }
            return(goAnim.Fire(delay, bullet, startAnchor, destination, bulletSpeed, arcHeight, testAnchor));
        }
示例#29
0
 /// <summary>
 /// Returns the registered Game Object near a given position
 /// </summary>
 public GameObjectAnimator VGOGet(Vector2 mapPos, float distance)
 {
     distance *= distance;
     CheckVGOsArrayDirty();
     for (int k = 0; k < vgosCount; k++)
     {
         GameObjectAnimator go = vgos[k];
         float d = FastVector.SqrDistanceByValue(go.currentMap2DLocation, mapPos); // Vector2.SqrMagnitude (go.currentMap2DLocation - mapPos);
         if (d <= distance)
         {
             return(go);
         }
     }
     return(null);
 }
示例#30
0
 /// <summary>
 /// Registers the game object in the viewport collection. It's position will be monitored by the viewport updates.
 /// </summary>
 public void VGORegisterGameObject(GameObjectAnimator o)
 {
     if (vgos == null || o == null)
     {
         return;
     }
     if (o.uniqueId == 0)
     {
         while (vgos.ContainsKey(++lastUniqueIdUsed))
         {
         }
         o.uniqueId = lastUniqueIdUsed;
     }
     vgos [o.uniqueId] = o;
 }