4.5.12. Complete source code
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using BetaCell.Debug;
using BetaCell.Util.GlobalInfo.Content;
using BetaCell.Environment.Camera;
using BetaCell.Util.GlobalInfo;
using BetaCell.Environment.Animation;
using BetaCell.Environment.Mesh;
using BetaCell.Environment.Light;
using BetaCell.Environment.Mesh.Content;
using BetaCell.Environment;
using BetaCell.Environment.Mesh.Visitors;
using BetaCell.Dynamic.Skinned;
using BetaCell.Environment.Terrain;
using BetaCell.Environment.Texture.Feeder;
using BetaCell.Effects;
namespace Starter
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
//Game attributes
GraphicsDeviceManager
graphics;
GraphicsDevice device;
ContentManager content;
BCFirstPersonHumanCamera
camera;
//end game attributes
//This tutorial's attributes
BCMesh evilMesh;
BCAnimationSetBase
animationSet;
BCAnimationBase idle;
BCAnimationBase run;
BCAnimationBase idleToRun;
BCAnimationBase runToIdle;
BCAnimationController
controller;
BCAddSequenceListener
animationSequence;
//----------------------------
//Animation Control
Attributes
//----------------------------
double timeToChange = 5;
double lastTime = 0;
bool isIdle = true;
//----------------------------
//Env Map Attributes
//----------------------------
BCSuroundingBox surrounding;
TextureCube environment;
BCEnvMapFeeder envMapFeeder;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
content = new ContentManager(Services);
#if(XBOX360)
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
#else
graphics.PreferredBackBufferWidth = 852;
graphics.PreferredBackBufferHeight = 480;
#endif
}
protected override void
Initialize()
{
//base initialization
device = graphics.GraphicsDevice;
BCLogger.instance.init(device, content);
BCInitializationManager.initialize(content);
OnActivated(null, null);
//-----
//MESHES
//-----
evilMesh =
BCMeshReader.readMesh("Content/gameAssets/evilAnimatedTexturedB", null,
device, content);
//-----
//Light Effect
//-----
BCBasicLight light = new BCBasicLight();
light.range = 3000;
light.transform = Matrix.CreateTranslation(-1f, -1f, -1f);
light.cubAtten = 0;
light.constAtten = 1;
light.linAtten = 0;
light.useShadows = false;
BCDynamicEffectVisitor lightSetter = new BCDynamicEffectVisitor(
light.getFunction("Shader.Light.Classic.PixelDirectionLight"), light, 1
);
evilMesh.visit(lightSetter);
//---------
//ANIMATION
//---------
animationSet =
content.Load<BCAnimationSetBase>("Content/gameAssets/evilAnimatedTexturedBAnim");
animationSet.animations.TryGetValue("Idle", out idle);
animationSet.animations.TryGetValue("Run", out run);
animationSet.animations.TryGetValue("IdleToRun", out idleToRun);
animationSet.animations.TryGetValue("RunToIdle", out runToIdle);
controller = new BCAnimationController(evilMesh.skeleton.boneCount);
evilMesh.controller = controller;
BCSkinnedMeshFeeder skinnedFeeder = new BCSkinnedMeshFeeder();
BCDynamicEffectVisitor skinnedSetter = new BCDynamicEffectVisitor(
skinnedFeeder.getFunction("Actuator.Skinned.SkinnedMesh"),
skinnedFeeder, 0
);
evilMesh.visit(skinnedSetter);
animationSequence = new BCAddSequenceListener();
animationSequence.setNextAnimation(null, idle);
controller.addSingle(idle, animationSequence, 0);
//---
//Env Map Effect
//---
environment =
content.Load<TextureCube>("Content/gameAssets/grassenvmap1024");
envMapFeeder = new BCEnvMapFeeder(environment,
new BCSampler("LINEAR", "LINEAR", "LINEAR", "4", "WRAP", "WRAP",
"0xffffff"));
BCDynamicEffectVisitor envSetter = new BCDynamicEffectVisitor(
envMapFeeder.getFunction("Shader.Texture.EnvMap.Basic"), envMapFeeder,
BCDynamicEffect.MAX_FUNCTIONS);
evilMesh.visit(envSetter);
base.Initialize();
}
protected override void
OnActivated(object sender, EventArgs args)
{
buildViewMatrix();
base.OnActivated(sender, args);
}
void buildViewMatrix()
{
Vector3 pos = new Vector3(6f, 2, 6f);
Vector3 look = new Vector3(-7, -3.0f, -7);
look.Normalize();
camera = new BCFirstPersonHumanCamera(look, pos,
MathHelper.PiOver4,
(float)this.Window.ClientBounds.Width /
(float)this.Window.ClientBounds.Height,
1f, 200);
//Surrounding Box
surrounding = new BCSuroundingBox(device, camera, environment);
BCGlobalInfo.instance.setMatrix(
BCGlobalInfo.VIEW_INDEX,
camera.getViewMatrix(-1)
);
BCGlobalInfo.instance.setMatrix(
BCGlobalInfo.PROJECTION_INDEX,
camera.getProjectionMatrix(-1)
);
}
protected override void
Update(GameTime gameTime)
{
double time = gameTime.TotalGameTime.TotalSeconds;
if
(time - lastTime > timeToChange)
{
lastTime = time;
if (isIdle)
{
if (animationSequence.setNextAnimation(idleToRun, run))
{
isIdle = false;
}
}
else
{
if (animationSequence.setNextAnimation(runToIdle, idle))
{
isIdle = true;
}
}
}
envMapFeeder.mirrorFactor = ((float)time / 10) % 2;
if
(envMapFeeder.mirrorFactor > 1)
{
envMapFeeder.mirrorFactor = 2 - envMapFeeder.mirrorFactor;
}
controller.update(time);
}
protected override void
Draw(GameTime gameTime)
{
surrounding.draw(gameTime, 0);
evilMesh.Draw(gameTime);
BCLogger.instance.printFPS(gameTime);
BCLogger.instance.flush();
base.Draw(gameTime);
}
}
}