예제 #1
0
 public virtual void OnCollision(GameObject collider, GameObject collidable, GameScene scene, Vector3 position,
                                 Vector3 normal) =>
 OnCollision(new CollisionEventArgs(position, normal, collider, collidable, scene));
예제 #2
0
        /// <summary>
        /// Checks if there is a <see cref="Model"/> to draw and draws it.
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="spriteBatch"></param>
        protected virtual void Draw(GameScene scene, Matrix view, Matrix projection)
        {
            if (this.Model == null)
            {
                return;                                 // No model means it can't be rendered.
            }
            // copy the scale of bones from the model to apply it later.
            var transformMatrices = new Matrix[this.Model.Bones.Count];

            this.Model.CopyAbsoluteBoneTransformsTo(transformMatrices);

            var originalRastState = scene.Game.GraphicsDevice.RasterizerState;

            if (!CullingEnabled)
            {
                var newRastState = new RasterizerState
                {
                    CullMode             = CullMode.None,
                    DepthBias            = originalRastState.DepthBias,
                    DepthClipEnable      = originalRastState.DepthClipEnable,
                    FillMode             = originalRastState.FillMode,
                    MultiSampleAntiAlias = originalRastState.MultiSampleAntiAlias,
                    ScissorTestEnable    = originalRastState.ScissorTestEnable,
                    SlopeScaleDepthBias  = originalRastState.SlopeScaleDepthBias
                };

                scene.Game.GraphicsDevice.RasterizerState = newRastState;
            }

            foreach (ModelMesh mesh in this.Model.Meshes)
            {
                if (this.Effect == null)
                {
                    foreach (Effect effect in mesh.Effects)
                    {
                        if (!(effect is BasicEffect))
                        {
                            continue;
                        }

                        BasicEffect basisEffect = (BasicEffect)effect;

                        // calculating the full rotation of our object.
                        //Console.WriteLine($"POS: {this.GetHierarchyPosition().X} {this.GetHierarchyPosition().Y} {this.GetHierarchyPosition().Z}");

                        basisEffect.World = transformMatrices[mesh.ParentBone.Index]
                                            * this.ScaleMatrix
                                            * this.RotationMatrix
                                            * Matrix.CreateTranslation(this.GetHierarchyPosition());

                        basisEffect.View       = view;
                        basisEffect.Projection = projection;

                        scene.AddLightningToEffect(basisEffect);
                    }
                }
                else
                {
                    foreach (var part in mesh.MeshParts)
                    {
                        part.Effect = this.Effect;

                        this.EffectParams.Invoke(this, part.Effect, transformMatrices, mesh, scene);
                        if (ApplySceneLight)
                        {
                            scene.AddLightningToEffect(part.Effect);
                        }
                    }
                }

                mesh.Draw();
            }

            if (!CullingEnabled)
            {
                scene.Game.GraphicsDevice.RasterizerState = originalRastState;
            }
        }
예제 #3
0
        /// <summary>
        /// Checks if there is a <see cref="Model"/> to draw and draws it with specified Effect.
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="spriteBatch"></param>
        protected virtual void DrawWithSpecificEffect(GameScene scene, Matrix view, Matrix projection, Effect effect, Action <GameObject, Effect, Matrix[], ModelMesh, GameScene> effectParams, bool applySceneLighting, string technique = null)
        {
            if (this.Model == null)
            {
                return;                                 // No model means it can't be rendered.
            }
            // copy the scale of bones from the model to apply it later.
            var transformMatrices = new Matrix[this.Model.Bones.Count];

            this.Model.CopyAbsoluteBoneTransformsTo(transformMatrices);

            var originalRastState = scene.Game.GraphicsDevice.RasterizerState;

            if (!CullingEnabled)
            {
                var newRastState = new RasterizerState
                {
                    CullMode             = CullMode.None,
                    DepthBias            = originalRastState.DepthBias,
                    DepthClipEnable      = originalRastState.DepthClipEnable,
                    FillMode             = originalRastState.FillMode,
                    MultiSampleAntiAlias = originalRastState.MultiSampleAntiAlias,
                    ScissorTestEnable    = originalRastState.ScissorTestEnable,
                    SlopeScaleDepthBias  = originalRastState.SlopeScaleDepthBias
                };

                scene.Game.GraphicsDevice.RasterizerState = newRastState;
            }

            if (technique == null)
            {
                foreach (var pass in effect.CurrentTechnique.Passes)
                {
                    foreach (var mesh in Model.Meshes)
                    {
                        foreach (var part in mesh.MeshParts)
                        {
                            part.Effect = effect;
                            effectParams.Invoke(this, part.Effect, transformMatrices, mesh, scene);
                            if (applySceneLighting)
                            {
                                scene.AddLightningToEffect(part.Effect);
                            }
                        }
                        mesh.Draw();
                    }
                }
            }
            else
            {
                foreach (var pass in effect.Techniques[technique].Passes)
                {
                    foreach (var mesh in Model.Meshes)
                    {
                        foreach (var part in mesh.MeshParts)
                        {
                            part.Effect = effect;
                            effectParams.Invoke(this, part.Effect, transformMatrices, mesh, scene);
                            if (applySceneLighting)
                            {
                                scene.AddLightningToEffect(part.Effect);
                            }
                        }
                        mesh.Draw();
                    }
                }
            }

            if (!CullingEnabled)
            {
                scene.Game.GraphicsDevice.RasterizerState = originalRastState;
            }
        }
예제 #4
0
 /// <summary>
 /// Calls <seealso cref="Draw"/>
 /// After that draws all <see cref="ChildObjects"/>
 /// </summary>
 /// <param name="scene"></param>
 /// <param name="spriteBatch"></param>
 public void DrawLogic(GameScene scene, SpriteBatch spriteBatch, GameObjectDrawMode drawMode = GameObjectDrawMode.All)
 {
     DrawLogic(scene, spriteBatch, scene.GetViewMatrix(), scene.GetProjectionMatrix(), drawMode);
 }
예제 #5
0
 /// <summary>
 /// Updates the <see cref="GameObject"/>
 /// </summary>
 /// <param name="scene"></param>
 /// <param name="gameTime"></param>
 public abstract void Update(GameScene scene, GameTime gameTime);
예제 #6
0
 /// <summary>
 /// Adds a <seealso cref="GameScene"/> to the <see cref="SceneStack"/>
 /// </summary>
 /// <param name="scene"></param>
 /// <param name="entranceId"></param>
 public static void AddSceneToStack(GameScene scene, int entranceId = 0) => Instance._AddSceneToStack(scene, entranceId);
예제 #7
0
 /// <summary>
 /// Remove CurrentScene and add new Scene to Stack
 /// </summary>
 /// <param name="newScene"></param>
 /// <param name="entranceId"></param>
 /// <param name="clearStack"></param>
 public static void ChangeScene(GameScene newScene, int entranceId = 0, bool clearStack = false) =>
 Instance._ChangeScene(newScene, entranceId, clearStack);
예제 #8
0
 /// <summary>
 /// Registers a <seealso cref="GameScene"/> so it can be added to the <see cref="SceneStack"/><para/>
 /// Required before using <seealso cref="AddSceneToStack(String)"/>
 /// Returns Key of GameScene
 /// </summary>
 /// <param name="scene"></param>
 /// <param name="behavior"></param>
 /// <returns></returns>
 public static string RegisterScene(GameScene scene, RegisterBehavior behavior = RegisterBehavior.Ignore) => Instance._RegisterScene(scene, behavior);