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;
namespace Peanuts
{
///
/// This is the main type for your game
///
public class PeanutsGame : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private PeanutsModel model;
private PeanutsView view;
private PeanutsControl controller;
private Texture2D uniformTexture;
private SpriteFont font;
private ICollection keysDown;
private ICollection keysToCheck;
public PeanutsGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
///
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
///
protected override void Initialize()
{
// set up view and projection for camera beyond bottom (y = 1.0) edge of playfield
// and a little above the field (should really ask the model about its size instead
// of hard-coding this)
Matrix viewMatrix = Matrix.CreateLookAt(new Vector3(0.5f, 3.0f, -2.0f),
new Vector3(0.5f, 0.0f, 0.0f),
new Vector3(0.0f, 0.0f, -1.0f));
float aspect = (float)Window.ClientBounds.Width / (float)Window.ClientBounds.Height;
// set up perspective projection with 45 degree field of view and enough depth
// to get everything in our scene
Matrix projectionMatrix = Matrix.CreatePerspectiveFieldOfView((float)Math.PI / 6, aspect, 0.1f, 10.0f);
// set up effect with the above view and projection, and with directional light aimed from over
// player's right shoulder
BasicEffect effect = new BasicEffect(GraphicsDevice, null);
effect.World = Matrix.Identity;
effect.View = viewMatrix;
effect.Projection = projectionMatrix;
effect.VertexColorEnabled = true;
effect.LightingEnabled = true;
effect.DirectionalLight0.Enabled = true;
Vector3 lightDir = new Vector3(1.0f, -1.0f, 1.0f);
lightDir.Normalize();
effect.DirectionalLight0.Direction = lightDir;
effect.DirectionalLight0.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
effect.DirectionalLight0.SpecularColor = new Vector3(1.0f, 1.0f, 1.0f);
model = new PeanutsModel();
view = new PeanutsView(model, effect, GraphicsDevice);
controller = new PeanutsControl();
// set up event handling
controller.Climb += new PeanutsControl.ClimbHandler(model.ClimbPlayer);
keysDown = new HashSet();
keysToCheck = new HashSet();
keysToCheck.Add(Keys.Space);
base.Initialize();
}
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
uniformTexture = Content.Load("blank");
font = Content.Load("Arial");
view.SetFont(font);
}
///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///
/// Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// check keys that we always care about the state of
KeyboardState kb = Keyboard.GetState();
int dx = 0;
int dy = 0;
if (kb.IsKeyDown(Keys.Up))
{
dy = -1;
}
if (kb.IsKeyDown(Keys.Down))
{
dy = 1;
}
if (kb.IsKeyDown(Keys.Left))
{
dx = -1;
}
if (kb.IsKeyDown(Keys.Right))
{
dx = 1;
}
model.MovePlayer(dx, dy);
// check keys that we only care about when they are first pressed
foreach (Keys k in keysToCheck)
{
if (kb.IsKeyDown(k))
{
if (!keysDown.Contains(k))
{
// key is down, wasn't down before
keysDown.Add(k);
controller.keyPressed(k);
}
}
}
// update the keys that are down
ICollection upKeys = new HashSet();
foreach (Keys k in keysDown)
{
if (kb.IsKeyUp(k))
{
upKeys.Add(k);
// controller.keyReleased(k);
}
}
foreach (Keys k in upKeys)
{
keysDown.Remove(k);
}
// update the model
model.Update();
base.Update(gameTime);
}
///
/// This is called when the game should draw itself.
///
/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
view.Draw();
base.Draw(gameTime);
}
}
}