Example #1
0
        private void CreateTowers(
            ContentManager contentManager,
            GameObjectCollection gameObjects,
            GameObject gameObject,
            MapObject mapObject)
        {
            var towers = new List <GameObject>();

            void CreateTower(string objectName, float x, float y)
            {
                var tower = AddDisposable(contentManager.InstantiateObject(objectName));

                var offset            = new Vector3(x, y, 0);
                var transformedOffset = Vector3.Transform(offset, gameObject.Transform.Rotation);

                tower.Transform.Translation = gameObject.Transform.Translation + transformedOffset;
                tower.Transform.Rotation    = gameObject.Transform.Rotation;

                gameObjects.Add(tower);
            }

            var landmarkBridgeTemplate = GetBridgeTemplate(contentManager, mapObject);

            var halfLength = gameObject.Definition.Geometry.MinorRadius;
            var halfWidth  = gameObject.Definition.Geometry.MajorRadius;

            CreateTower(landmarkBridgeTemplate.TowerObjectNameFromLeft, -halfWidth, -halfLength);
            CreateTower(landmarkBridgeTemplate.TowerObjectNameFromRight, halfWidth, -halfLength);
            CreateTower(landmarkBridgeTemplate.TowerObjectNameToLeft, -halfWidth, halfLength);
            CreateTower(landmarkBridgeTemplate.TowerObjectNameToRight, halfWidth, halfLength);
        }
Example #2
0
        public ObjectDefinitionView(AssetViewContext context, ObjectDefinition objectDefinition)
            : base(context)
        {
            var gameObjects = new GameObjectCollection(context.Game.ContentManager);

            _gameObject = gameObjects.Add(objectDefinition, context.Game.CivilianPlayer);

            _modelConditionStates = _gameObject.ModelConditionStates.ToList();
            _selectedIndex        = 0;

            context.Game.Scene3D = new Scene3D(
                context.Game,
                new ArcballCameraController(Vector3.Zero, 200),
                null,
                null,
                Array.Empty <Terrain.WaterArea>(),
                Array.Empty <Terrain.Road>(),
                Array.Empty <Terrain.Bridge>(),
                null,
                gameObjects,
                new WaypointCollection(),
                new WaypointPathCollection(),
                WorldLighting.CreateDefault(),
                Array.Empty <Player>(),
                Array.Empty <Team>());
        }
Example #3
0
        public BridgeTowers(
            BridgeTemplate template,
            GameObjectCollection gameObjects,
            Matrix4x4 worldMatrix,
            float startX,
            float startY,
            float endX,
            float endY,
            Quaternion rotation)
        {
            void CreateTower(ObjectDefinition objectDefinition, float x, float y)
            {
                var tower = gameObjects.Add(objectDefinition);

                tower.Transform.Translation = Vector3.Transform(
                    new Vector3(x, y, 0),
                    worldMatrix);

                tower.Transform.Rotation = rotation;
            }

            CreateTower(template.TowerObjectNameFromLeft.Value, startX, startY);
            CreateTower(template.TowerObjectNameFromRight.Value, endX, startY);
            CreateTower(template.TowerObjectNameToLeft.Value, startX, endY);
            CreateTower(template.TowerObjectNameToRight.Value, endX, endY);
        }
Example #4
0
        public void Seed(GameObjectCollection scene)
        {
            var prov = new CameraProvider();

            prov.Camera.Fov          = MathUtil.DegreesToRadians(90);
            prov.Camera.DrawDistance = 100;
            prov.Camera.NearDistance = 0.001f;
            prov.Camera.Position     = new Vector3(0, 0, -20);
            prov.Camera.Rotation     = Quaternion.RotationLookAtLH(Vector3.ForwardLH, Vector3.Up);

            //var tmp = MeshLoader.LoadScene(@"Volkswagen.fbx");

            //var go = GameObjectFactory.Create(new FbxObjectCreator(@"3_cubes (3).fbx"));
            //var go = GameObjectFactory.Create(new FbxObjectCreator(@"ZF_YUP.fbx"));

            //var go = new GameObject();

            /*var tmp = MeshLoader.LoadScene(@"personfbx_-Y_Z.fbx");
             *
             * go.Children.Add(new GameObject());
             * go.Children.Add(new GameObject());
             * go.Children.Add(new GameObject());
             *
             * go.Children[0].AddComponent<MeshRenderer>().Initialize(tmp[0].NodeMeshes[0], "vx1", "px1");
             * go.Children[1].AddComponent<MeshRenderer>().Initialize(tmp[1].NodeMeshes[0], "vx1", "px1");
             * go.Children[2].AddComponent<MeshRenderer>().Initialize(tmp[2].NodeMeshes[0], "vx1", "px1");
             *
             * go.Children[0].Position = tmp[0].Position;
             * go.Children[0].Rotation = tmp[0].Rotation;
             * go.Children[0].Scale = tmp[0].Scale;
             *
             * go.Children[1].Position = tmp[1].Position;
             * go.Children[1].Rotation = tmp[1].Rotation;
             * go.Children[1].Scale = tmp[1].Scale;
             *
             * go.Children[2].Position = tmp[2].Position;
             * go.Children[2].Rotation = tmp[2].Rotation;
             * go.Children[2].Scale = tmp[2].Scale;*/
            //personfbx_-Y_Z.fbx
            var light = new GameObject();
            var dir   = light.AddComponent <DirectionLight>();

            dir.Color     = Color3.White;
            dir.Direction = Vector3.ForwardLH;
            scene.Add(light);

            var go = GameObjectFactory.CreateAndRegister(new FbxObjectCreator(@"virt_ice_scene.fbx"));

            //go.Scale = new Vector3(0.1f);
            go.AddScript <TestScript>();

            //var meshRend = go.Children["Стол"].GetComponent<MeshRenderer>();
            //go.GetComponent<Rigidbody>().TriggerEnter += (o, x) => System.Console.WriteLine("trigger");
        }
Example #5
0
        private static void LoadObjects(
            ContentManager contentManager,
            HeightMap heightMap,
            MapObject[] mapObjects,
            Team[] teams,
            out WaypointCollection waypointCollection,
            out GameObjectCollection gameObjects)
        {
            var waypoints = new List <Waypoint>();

            gameObjects = new GameObjectCollection(contentManager);

            foreach (var mapObject in mapObjects)
            {
                switch (mapObject.RoadType)
                {
                case RoadType.None:
                    var position = mapObject.Position;

                    switch (mapObject.TypeName)
                    {
                    case "*Waypoints/Waypoint":
                        waypoints.Add(CreateWaypoint(mapObject));
                        break;

                    default:
                        // TODO: Handle locomotors when they're implemented.
                        position.Z += heightMap.GetHeight(position.X, position.Y);

                        var gameObject = CreateGameObject(mapObject, teams, contentManager);

                        if (gameObject != null)
                        {
                            gameObject.Transform.Translation = position;
                            gameObject.Transform.Rotation    = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, mapObject.Angle);

                            gameObjects.Add(gameObject);
                        }

                        break;
                    }
                    break;

                default:
                    // TODO: Roads.
                    break;
                }

                contentManager.GraphicsDevice.WaitForIdle();
            }

            waypointCollection = new WaypointCollection(waypoints);
        }
Example #6
0
        public ObjectDefinitionView(Func <IntPtr, Game> createGame, ObjectDefinition objectDefinition)
        {
            var listBox = new ListBox
            {
                Width           = 200,
                ItemTextBinding = Binding.Property((BitArray <ModelConditionFlag> x) => x.DisplayName)
            };

            Panel1 = listBox;

            Panel2 = new GameControl
            {
                CreateGame = h =>
                {
                    var game = createGame(h);

                    var gameObjects = new GameObjectCollection(game.ContentManager);
                    var gameObject  = gameObjects.Add(objectDefinition);

                    listBox.SelectedValueChanged += (sender, e) =>
                    {
                        var modelConditionState = (BitArray <ModelConditionFlag>)listBox.SelectedValue;
                        gameObject.SetModelConditionFlags(modelConditionState);
                    };
                    listBox.DataStore     = gameObject.ModelConditionStates.ToList();
                    listBox.SelectedIndex = 0;

                    game.Scene3D = new Scene3D(
                        game,
                        new ArcballCameraController(Vector3.Zero, 200),
                        null,
                        null,
                        null,
                        gameObjects,
                        new WaypointCollection(),
                        new WaypointPathCollection(),
                        WorldLighting.CreateDefault(),
                        Array.Empty <Player>(),
                        Array.Empty <Team>());

                    return(game);
                }
            };
        }
        public void Apply(ClientGame game, GameTime gameTime)
        {
            this.ResetReader();
            Type typeFromMessage       = GameObjectTypes.GetType(this.ReadInt());
            int  idFromMessage         = this.ReadInt();
            bool isDesroyedFromMessage = this.ReadBoolean();

            GameObjectCollection collection = game.GameObjectCollection;

            GameObject obj;

            if (collection.Contains(idFromMessage))
            {
                obj = collection.Get(idFromMessage);
                if (obj.LastMessageTimeStamp > this.TimeStamp)
                {
                    return;
                }
            }
            else
            {
                if (isDesroyedFromMessage)
                {
                    return;
                }
                obj = GameObjectTypes.Construct(typeFromMessage, game, idFromMessage);
                collection.Add(obj);
            }

            if (!(obj.GetType() == typeFromMessage && obj.ID == idFromMessage))
            {
                throw new Exception("this message does not belong to this object");
            }

            obj.IsDestroyed = isDesroyedFromMessage;
            foreach (GameObjectField field in obj.Fields)
            {
                field.ApplyMessage(this);
            }
            this.AssertMessageEnd();

            obj.LatencyAdjustment(gameTime, this.TimeStamp);
        }
Example #8
0
        protected override void LoadContent()
        {
            SpriteBatch = new SpriteBatch(GraphicsDevice);

            PreloadContent();

            backgroundObjects = new GameObjectCollection();
            foreach (var obj in BackgroundObjects)
                backgroundObjects.Add(obj);

            foregroundObjects = new GameObjectCollection();
            foreach (var obj in ForegroundObjects)
                foregroundObjects.Add(obj);

            SceneManager.LoadDefaultScene();
        }
Example #9
0
        private void LoadObjects(
            AssetLoadContext loadContext,
            Player civilianPlayer,
            HeightMap heightMap,
            MapObject[] mapObjects,
            List <Team> teams,
            out WaypointCollection waypointCollection,
            out GameObjectCollection gameObjects,
            out RoadCollection roads,
            out Bridge[] bridges)
        {
            var waypoints = new List <Waypoint>();

            gameObjects = AddDisposable(new GameObjectCollection(loadContext, civilianPlayer));
            var roadsList   = new List <Road>();
            var bridgesList = new List <Bridge>();

            var roadTopology = new RoadTopology();

            for (var i = 0; i < mapObjects.Length; i++)
            {
                var mapObject = mapObjects[i];

                var position = mapObject.Position;

                switch (mapObject.RoadType & RoadType.PrimaryType)
                {
                case RoadType.None:
                    switch (mapObject.TypeName)
                    {
                    case "*Waypoints/Waypoint":
                        waypoints.Add(new Waypoint(mapObject));
                        break;

                    default:
                        position.Z += heightMap.GetHeight(position.X, position.Y);

                        var gameObject = GameObject.FromMapObject(mapObject, teams, loadContext.AssetStore, gameObjects, position);
                        if (gameObject != null)
                        {
                            gameObjects.Add(gameObject);
                        }

                        break;
                    }
                    break;

                case RoadType.BridgeStart:
                case RoadType.BridgeEnd:
                    // Multiple invalid bridges can be found in e.g GLA01.
                    if ((i + 1) >= mapObjects.Length || !mapObjects[i + 1].RoadType.HasFlag(RoadType.BridgeEnd))
                    {
                        Logger.Warn($"Invalid bridge: {mapObject.ToString()}, skipping...");
                        continue;
                    }

                    var bridgeEnd = mapObjects[++i];

                    bridgesList.Add(AddDisposable(new Bridge(
                                                      loadContext,
                                                      heightMap,
                                                      mapObject,
                                                      mapObject.Position,
                                                      bridgeEnd.Position,
                                                      gameObjects)));

                    break;

                case RoadType.Start:
                case RoadType.End:
                    var roadEnd = mapObjects[++i];

                    // Some maps have roads with invalid start- or endpoints.
                    // We'll skip processing them altogether.
                    if (mapObject.TypeName == "" || roadEnd.TypeName == "")
                    {
                        Logger.Warn($"Road {mapObject.ToString()} has invalid start- or endpoint, skipping...");
                        continue;
                    }

                    if (!mapObject.RoadType.HasFlag(RoadType.Start) || !roadEnd.RoadType.HasFlag(RoadType.End))
                    {
                        throw new InvalidDataException();
                    }

                    // Note that we're searching with the type of either end.
                    // This is because of weirdly corrupted roads with unmatched ends in USA04, which work fine in WB and SAGE.
                    var roadTemplate =
                        loadContext.AssetStore.RoadTemplates.GetByName(mapObject.TypeName)
                        ?? loadContext.AssetStore.RoadTemplates.GetByName(roadEnd.TypeName);

                    if (roadTemplate == null)
                    {
                        throw new InvalidDataException($"Missing road template: {mapObject.TypeName}");
                    }

                    roadTopology.AddSegment(roadTemplate, mapObject, roadEnd);
                    break;
                }

                loadContext.GraphicsDevice.WaitForIdle();
            }

            roads = AddDisposable(new RoadCollection(roadTopology, loadContext, heightMap));
            waypointCollection = new WaypointCollection(waypoints);
            bridges            = bridgesList.ToArray();
        }
Example #10
0
        private void LoadObjects(
            ContentManager contentManager,
            HeightMap heightMap,
            MapObject[] mapObjects,
            Team[] teams,
            out WaypointCollection waypointCollection,
            out GameObjectCollection gameObjects,
            out Road[] roads,
            out Bridge[] bridges)
        {
            var waypoints = new List <Waypoint>();

            gameObjects = new GameObjectCollection(contentManager);
            var roadsList   = new List <Road>();
            var bridgesList = new List <Bridge>();

            var roadTopology = new RoadTopology();

            for (var i = 0; i < mapObjects.Length; i++)
            {
                var mapObject = mapObjects[i];

                var position = mapObject.Position;

                switch (mapObject.RoadType & RoadType.PrimaryType)
                {
                case RoadType.None:
                    switch (mapObject.TypeName)
                    {
                    case "*Waypoints/Waypoint":
                        waypoints.Add(CreateWaypoint(mapObject));
                        break;

                    default:
                        position.Z += heightMap.GetHeight(position.X, position.Y);

                        var gameObject = CreateGameObject(mapObject, teams, contentManager);

                        if (gameObject != null)
                        {
                            gameObject.Transform.Translation = position;
                            gameObject.Transform.Rotation    = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, mapObject.Angle);

                            gameObjects.Add(gameObject);

                            if (gameObject.Definition.IsBridge)
                            {
                                // This is a landmark bridge. We need to add towers at the corners.
                                CreateTowers(contentManager, gameObjects, gameObject, mapObject);
                            }
                        }

                        break;
                    }
                    break;

                case RoadType.BridgeStart:
                case RoadType.BridgeEnd:
                    // Multiple invalid bridges can be found in e.g GLA01.
                    // TODO: Log a warning.
                    if ((i + 1) >= mapObjects.Length || !mapObjects[i + 1].RoadType.HasFlag(RoadType.BridgeEnd))
                    {
                        continue;
                    }

                    var bridgeEnd = mapObjects[++i];

                    var bridgeTemplate = GetBridgeTemplate(contentManager, mapObject);

                    if (Bridge.TryCreateBridge(
                            contentManager,
                            heightMap,
                            bridgeTemplate,
                            mapObject.Position,
                            bridgeEnd.Position,
                            out var bridge))
                    {
                        bridgesList.Add(AddDisposable(bridge));
                    }


                    break;

                default:
                    var roadEnd = mapObjects[++i];

                    // Some maps have roads with invalid start- or endpoints.
                    // We'll skip processing them altogether.
                    // TODO: Log a warning.
                    if (mapObject.TypeName == "" || roadEnd.TypeName == "")
                    {
                        continue;
                    }

                    if (!mapObject.RoadType.HasFlag(RoadType.Start) || !roadEnd.RoadType.HasFlag(RoadType.End))
                    {
                        throw new InvalidDataException();
                    }

                    // Note that we're searching with the type of either end.
                    // This is because of weirdly corrupted roads with unmatched ends in USA04, which work fine in WB and SAGE.
                    var roadTemplate = contentManager.IniDataContext.RoadTemplates.Find(x =>
                                                                                        x.Name == mapObject.TypeName || x.Name == roadEnd.TypeName);

                    if (roadTemplate == null)
                    {
                        throw new InvalidDataException($"Missing road template: {mapObject.TypeName}");
                    }

                    roadTopology.AddSegment(roadTemplate, mapObject, roadEnd);
                    break;
                }

                contentManager.GraphicsDevice.WaitForIdle();
            }

            // The map stores road segments with no connectivity:
            // - a segment is from point A to point B
            // - with a road type name
            // - and start and end curve types (angled, tight curve, broad curve).

            // The goal is to create road networks of connected road segments,
            // where a network has only a single road type.

            // A road network is composed of 2 or more nodes.
            // A network is a (potentially) cyclic graph.

            // A road node has > 1 and <= 4 edges connected to it.
            // A node can be part of multiple networks.

            // An edge can only exist in one network.

            // TODO: If a node stored in the map has > 4 edges, the extra edges
            // are put into a separate network.

            var networks = roadTopology.BuildNetworks();

            foreach (var network in networks)
            {
                foreach (var edge in network.Edges)
                {
                    var startPosition = edge.Start.TopologyNode.Position;
                    var endPosition   = edge.End.TopologyNode.Position;

                    startPosition.Z += heightMap.GetHeight(startPosition.X, startPosition.Y);
                    endPosition.Z   += heightMap.GetHeight(endPosition.X, endPosition.Y);

                    roadsList.Add(AddDisposable(new Road(
                                                    contentManager,
                                                    heightMap,
                                                    edge.TopologyEdge.Template,
                                                    startPosition,
                                                    endPosition)));
                }
            }

            waypointCollection = new WaypointCollection(waypoints);
            roads   = roadsList.ToArray();
            bridges = bridgesList.ToArray();
        }
Example #11
0
 private void OnEnable()
 {
     _targetCollection.Add(gameObject);
 }
Example #12
0
 protected virtual void Awake()
 {
     targetCollection.Add(gameObject);
 }
Example #13
0
 public void OnEnable()
 {
     CarSpawners.Add(gameObject);
 }
Example #14
0
 private void OnEnable()
 {
     InstantiatedCars.Add(gameObject);
 }