/// <summary> /// Call this to initialize an GameObject with data supplied in a file. /// </summary> /// <param name="fileName">The file to load from.</param> public virtual void LoadContent(String fileName) { // Give this object a unique id and increment the counter so that the next // object gets a unique id as well. mID = mUniqueIDCounter++; mDirection = new Direction(); mFactoryInfo = new GameObjectFactory.FactoryInfo(); mClassifications = new List <GameObjectDefinition.Classifications>(); mCollisionRectangle = new Math.Rectangle(); mRenderRectangle = new Math.Rectangle(); mTemplateFileName = fileName; if (null != fileName) { GameObjectDefinition def = GameObjectManager.pInstance.pContentManager.Load <GameObjectDefinition>(fileName); mRenderPriority = def.mRenderPriority; mDoUpdate = def.mDoUpdate; mDoRender = def.mDoRender; mPosition = def.mPosition; mRotation = def.mRotation; mScale = def.mScale; mIsStatic = def.mIsStatic; mCollisionRectangle = new Math.Rectangle(def.mCollisionBoxDimensions); mCollisionRectangle.pCenterPoint = mPosition; // Being lazy for now. Just assume that a scaler of collision box is big enough to always show character. mRenderRectangle = new Math.Rectangle(def.mCollisionBoxDimensions * 4f); mRenderRectangle.pCenterPoint = mPosition; mMotionRoot = def.mMotionRoot; if (def.mCollisionRoot == null) { mCollisionRoot = Vector2.Zero; } else { mCollisionRoot = def.mCollisionRoot; } for (Int32 i = 0; def.mClassifications != null && i < def.mClassifications.Count; i++) { mClassifications.Add(def.mClassifications[i]); } mBlendMode = def.mBlendMode; for (Int32 i = 0; i < def.mBehaviourFileNames.Count; i++) { String goRootPath = System.IO.Path.GetDirectoryName(fileName); Behaviour.Behaviour temp = CreateBehaviourByName(def.mBehaviourClassNames[i], goRootPath + "\\Behaviours\\" + def.mBehaviourFileNames[i]); mBehaviours.Add(temp); } } else { mRenderPriority = 50; mDoUpdate = true; mDoRender = true; mBlendMode = GameObjectDefinition.BlendMode.STANDARD; } }
/// <summary> /// Should be called once per render update. /// </summary> /// <param name="batch">Where sprites will be rendered to.</param> /// <param name="showDebugInfo">True if debug information should be shown this frame.</param> public void Render(SpriteBatch batch, Boolean showDebugInfo) { mGameObjects.Sort(CompareByRenderPriority); // Keep track of the blend modes so that we can detect when it needs to change. GameObjectDefinition.BlendMode currentBlend = GameObjectDefinition.BlendMode.UNDEFINED; mLastNumObjectsRendered = 0; for (int i = 0; i < mGameObjects.Count; i++) { GameObject obj = mGameObjects[i]; GameObjectDefinition.BlendMode blend = obj.pBlendMode; if (obj.pDoRender == true) { if (obj.pCollisionRect.pDimensions == Vector2.Zero || CameraManager.pInstance.IsOnCamera(obj.pRenderRect)) { // Has the blend mode changed from the previous game object to this one? if (currentBlend != blend) { if (blend == GameObjectDefinition.BlendMode.UNDEFINED) { System.Diagnostics.Debug.Assert(false, "Attempting to rendering Game Object with UNDEFINED blend mode."); } // Did the last game object set the blend mode, meaning we have to end it? // This is true for the first game object being rendered. if (currentBlend != GameObjectDefinition.BlendMode.UNDEFINED) { batch.End(); } // Update the current blend mode so that we can tell when it changes. currentBlend = blend; if (blend == GameObjectDefinition.BlendMode.STANDARD) { batch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, null, null, null, mDefaultEffect, CameraManager.pInstance.pFinalTransform); // Keep the sprites looking crisp. batch.GraphicsDevice.SamplerStates[0] = mSpriteSamplerState; batch.GraphicsDevice.RasterizerState = mSpriteRasterState; } else if (blend == GameObjectDefinition.BlendMode.TEST) { // Keeping the TEST blend mode around because it will be handy for trying out new stuff, but // in the mean time we want to catch GameObjects using it by accident. System.Diagnostics.Debug.Assert(false, "BlendMode.TEST in use but not currently implemented."); } else if (blend == GameObjectDefinition.BlendMode.MULTIPLY) { batch.Begin(SpriteSortMode.Immediate, mMultiply, null, null, null, mDefaultEffect, CameraManager.pInstance.pFinalTransform); // Keep the sprites looking crisp. batch.GraphicsDevice.SamplerStates[0] = mSpriteSamplerState; batch.GraphicsDevice.RasterizerState = mSpriteRasterState; } else if (blend == GameObjectDefinition.BlendMode.MULTIPLY_UI) { // Use the Multiply blend mode but ignore the camera, so that it renders in screen // space. batch.Begin(SpriteSortMode.Immediate, mMultiply, null, null, null, mDefaultEffect); // Keep the sprites looking crisp. batch.GraphicsDevice.SamplerStates[0] = mSpriteSamplerState; batch.GraphicsDevice.RasterizerState = mSpriteRasterState; } else if (blend == GameObjectDefinition.BlendMode.STANDARD_UI) { batch.Begin( SpriteSortMode.Immediate, BlendState.NonPremultiplied, null, null, null, mDefaultEffect, CameraManager.pInstance.pFinalTransformUI); // Keep the sprites looking crisp. batch.GraphicsDevice.SamplerStates[0] = mSpriteSamplerState; batch.GraphicsDevice.RasterizerState = mSpriteRasterState; } else if (blend != GameObjectDefinition.BlendMode.UNDEFINED) { System.Diagnostics.Debug.Assert(false, "Unhandled blend mode."); } } obj.Render(batch, mDefaultEffect); mLastNumObjectsRendered++; if (showDebugInfo) { DebugShapeDisplay.pInstance.AddAABB(obj.pCollisionRect, Color.Green); DebugShapeDisplay.pInstance.AddTransform(obj.pPosition); } } } } batch.End(); }