protected override void OnUpdate()
        {
            var deltaTime = Time.DeltaTime;

            Entities.ForEach((ref SmokeSpawner smokeSpawner, ref Car car, ref Translation translation,
                              ref LocalToWorld localToWorld) =>
            {
                smokeSpawner.SpawnTimer += deltaTime;
                if (smokeSpawner.SpawnTimer > smokeSpawner.SpawnInterval)
                {
                    smokeSpawner.SpawnTimer = 0f;
                    var scale      = car.IsEngineDestroyed ? Random.NextFloat(2f, 3.5f) : Random.NextFloat(0.5f, 1.2f);
                    var duration   = car.IsEngineDestroyed ? 2f : 0.45f;
                    var startAngle = Random.NextFloat(-180f, 180f);
                    Entity smokeEntity;

                    if (car.IsEngineDestroyed)
                    {
                        smokeEntity = EntityManager.Instantiate(PrefabReferences.carSmokeDestroyedPrefab);
                    }
                    else
                    {
                        smokeEntity = EntityManager.Instantiate(PrefabReferences.carSmokePrefab);
                    }

                    PostUpdateCommands.AddComponent(smokeEntity,
                                                    new Smoke {
                        Duration = duration, BaseScale = scale, Speed = 2f
                    });
                    PostUpdateCommands.AddComponent(smokeEntity, new Translation
                    {
                        Value =
                            translation.Value +
                            localToWorld.Right * smokeSpawner.SpawnOffset.x +
                            localToWorld.Up * smokeSpawner.SpawnOffset.y +
                            localToWorld.Forward * smokeSpawner.SpawnOffset.z
                    });
                    PostUpdateCommands.AddComponent(smokeEntity, new LocalToWorld());
                    PostUpdateCommands.AddComponent(smokeEntity,
                                                    new Rotation
                    {
                        Value = math.mul(quaternion.identity, quaternion.RotateZ(math.radians(startAngle)))
                    });
                    PostUpdateCommands.AddComponent(smokeEntity, new Scale {
                        Value = scale
                    });
#if UNITY_DOTSPLAYER
                    MeshRenderer meshRenderer = EntityManager.GetComponentData <MeshRenderer>(smokeEntity);
                    LitMaterial litMaterial   = EntityManager.GetComponentData <LitMaterial>(meshRenderer.material);

                    PostUpdateCommands.AddComponent(smokeEntity, litMaterial);
                    meshRenderer.material = smokeEntity;
                    PostUpdateCommands.AddComponent(smokeEntity, meshRenderer);
                    PostUpdateCommands.AddComponent(smokeEntity, new DynamicMaterial());
#endif
                }
            });
        }
Пример #2
0
        /// <summary>
        /// Initialize to implement per main type.
        /// </summary>
        override public void Initialize()
        {
            // create the scene
            GameScene scene = new GameScene();

            // create skybox
            Managers.GraphicsManager.CreateSkybox(null, scene.Root);
            Managers.Diagnostic.DebugRenderPhysics = false;

            // create camera object
            GameObject camera          = new GameObject();
            Camera     cameraComponent = new Camera();

            camera.AddComponent(cameraComponent);
            cameraComponent.LookAt    = new Vector3(100, 2, 100);
            camera.SceneNode.Position = new Vector3(0, 5, 0);
            camera.AddComponent(new CameraEditorController());
            camera.Parent = scene.Root;

            // create a tilemap for the floor
            TileMap tilemap = scene.Root.AddComponent(new TileMap(Vector2.One * 10f, 10)) as TileMap;

            // create floor material
            LitMaterial tilesMaterial = new LitMaterial();

            tilesMaterial.Texture        = Resources.GetTexture("game/floor");
            tilesMaterial.TextureEnabled = true;
            tilesMaterial.SpecularColor  = Color.Black;

            // set ambient light
            scene.Lights.AmbientLight = new Color(20, 20, 20, 255);

            // add robot model
            GameObject robot         = new GameObject();
            var        robotRenderer = robot.AddComponent(new ModelRenderer("Game/robot")) as ModelRenderer;
            var        robotMat      = new LitMaterial();

            robotMat.Texture        = Resources.GetTexture("Game/robottexture_0");
            robotMat.TextureEnabled = true;
            robotMat.DiffuseColor   = Color.White;
            robotRenderer.SetMaterial(robotMat);
            robot.SceneNode.Scale    = Vector3.One * tileSize * 0.75f;
            robot.SceneNode.Position = new Vector3(tilemapSize * tileSize * 0.5f, 10f, tilemapSize * tileSize * 0.5f);
            robot.Parent             = scene.Root;

            // attach light to camera
            {
                var lightComponent = camera.AddComponent(new Light()) as Light;
                lightComponent.Intensity = 2f;
                lightComponent.Range     = tileSize * 15f;
                lightComponent.Color     = Color.Green;
            }

            // add directional light
            {
                var lightComponent = scene.Root.AddComponent(new Light()) as Light;
                lightComponent.Intensity = 0.25f;
                lightComponent.Range     = 0f;
                lightComponent.Direction = Vector3.Normalize(new Vector3(0.2f, 1f, 0.1f));
                lightComponent.Color     = Color.White;
            }

            // attach lights
            lightsCenter      = new GameObject();
            lightsCenter.Name = "LightsCenter";
            lightsCenter.SceneNode.Position = robot.SceneNode.Position;
            for (int i = -1; i <= 1; i += 2)
            {
                GameObject lightGo = new GameObject();
                lightGo.Name = "Light" + i.ToString();
                lightGo.SceneNode.PositionX = i * tileSize * 3f;
                var lightComponent = lightGo.AddComponent(new Light()) as Light;
                lightComponent.Intensity = 3;
                lightComponent.Range     = tileSize * 15.5f;
                lightComponent.Color     = i == -1 ? Color.Red : Color.Blue;
                lightGo.Parent           = lightsCenter;
            }
            lightsCenter.Parent = scene.Root;

            // create some floor tiles
            for (int i = 0; i < tilemapSize; ++i)
            {
                for (int j = 0; j < tilemapSize; ++j)
                {
                    GameObject    tile      = tilemap.GetTile(new Point(i, j));
                    ShapeRenderer tileModel = tile.AddComponent(new ShapeRenderer(ShapeMeshes.Plane)) as ShapeRenderer;
                    tileModel.SetMaterial(tilesMaterial);
                    tile.SceneNode.Scale     = Vector3.One * tileSize * 0.5f;
                    tile.SceneNode.RotationX = (float)System.Math.PI * -0.5f;
                }
            }

            // add floor physical body
            GameObject floorPhysics   = new GameObject();
            float      wholePlaneSize = tilemapSize * tileSize;
            StaticBody floorBody      = floorPhysics.AddComponent(new StaticBody(new CollisionBox(wholePlaneSize, 5f, wholePlaneSize))) as StaticBody;

            floorBody.Restitution           = 0.75f;
            floorPhysics.SceneNode.Position = new Vector3((wholePlaneSize - tileSize) * 0.5f, -2.5f, (wholePlaneSize - tileSize) * 0.5f);
            floorPhysics.Parent             = scene.Root;

            // add diagnostic data paragraph to scene
            var diagnosticData = new GeonBit.UI.Entities.Paragraph("", GeonBit.UI.Entities.Anchor.BottomLeft, offset: Vector2.One * 10f, scale: 0.7f);

            diagnosticData.BeforeDraw = (GeonBit.UI.Entities.Entity entity) =>
            {
                diagnosticData.Text = Managers.Diagnostic.GetReportString();
            };
            scene.UserInterface.AddEntity(diagnosticData);

            // set scene
            GeonBitMain.Instance.Application.LoadScene(scene);
        }