Created
May 30, 2012 15:34
-
-
Save j0z/2837046 to your computer and use it in GitHub Desktop.
Top-Down Adventure game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Input; | |
using Microsoft.Xna.Framework.Graphics; | |
namespace The_Calling | |
{ | |
public class Camera | |
{ | |
public Matrix viewMatrix; | |
private Vector2 m_position; | |
private Vector2 m_halfViewSize; | |
public Camera(Rectangle clientRect) | |
{ | |
m_halfViewSize = new Vector2(clientRect.Width * 0.5f, clientRect.Height * 0.5f); | |
UpdateViewMatrix(); | |
} | |
public Vector2 Pos | |
{ | |
get | |
{ | |
return m_position; | |
} | |
set | |
{ | |
m_position = value; | |
UpdateViewMatrix(); | |
} | |
} | |
private void UpdateViewMatrix() | |
{ | |
viewMatrix = Matrix.CreateTranslation(m_halfViewSize.X - m_position.X, m_halfViewSize.Y - m_position.Y, 0.0f); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Input; | |
using Microsoft.Xna.Framework.Graphics; | |
namespace The_Calling | |
{ | |
class Cursor | |
{ | |
MouseState mouse = Mouse.GetState(); | |
public Vector2 position { get; set; } | |
Texture2D texture; | |
public Vector2 mousePosition; | |
public Cursor(Vector2 position, Texture2D texture) | |
{ | |
this.position = position; | |
this.texture = texture; | |
} | |
public void Update() | |
{ | |
mousePosition = new Vector2(mouse.X, mouse.Y); | |
} | |
public void Draw(SpriteBatch spriteBatch) | |
{ | |
spriteBatch.Draw(texture, mousePosition, Color.White); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
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.Media; | |
namespace The_Calling | |
{ | |
public class MainCalling : Microsoft.Xna.Framework.Game | |
{ | |
GraphicsDeviceManager graphics; | |
SpriteBatch spriteBatch; | |
Player player; | |
Rectangle clientRectangle; | |
Level level; | |
Cursor cursor; | |
Camera camera; | |
public static int screenWidth = 1280; | |
public static int screenHeight = 720; | |
public MainCalling() | |
{ | |
graphics = new GraphicsDeviceManager(this); | |
Content.RootDirectory = "Content"; | |
} | |
protected override void Initialize() | |
{ | |
this.IsMouseVisible = true; | |
graphics.PreferredBackBufferWidth = screenWidth; | |
graphics.PreferredBackBufferHeight = screenHeight; | |
graphics.ToggleFullScreen(); | |
base.Initialize(); | |
} | |
protected override void LoadContent() | |
{ | |
// Create a new SpriteBatch, which can be used to draw textures. | |
spriteBatch = new SpriteBatch(GraphicsDevice); | |
Texture2D temptexture = Content.Load<Texture2D>("charactertemp"); | |
clientRectangle = new Rectangle(0, 0, screenWidth, screenHeight); | |
player = new Player("Josh", 100, 100, temptexture); | |
temptexture = Content.Load<Texture2D>("arrow"); | |
cursor = new Cursor(player.position, temptexture); | |
temptexture = Content.Load<Texture2D>("background"); | |
level = new Level(temptexture); | |
camera = new Camera(clientRectangle); | |
} | |
protected override void UnloadContent() | |
{ | |
} | |
protected override void Update(GameTime gameTime) | |
{ | |
// Allows the game to exit | |
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) | |
this.Exit(); | |
if (Keyboard.GetState().IsKeyDown(Keys.Escape)) | |
this.Exit(); | |
player.Update(cursor); | |
camera.Pos = player.position; | |
cursor.position = player.position; | |
cursor.Update(); | |
base.Update(gameTime); | |
} | |
protected override void Draw(GameTime gameTime) | |
{ | |
GraphicsDevice.Clear(Color.CornflowerBlue); | |
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.viewMatrix); | |
level.Draw(spriteBatch); | |
player.Draw(spriteBatch); | |
spriteBatch.End(); | |
spriteBatch.Begin(); | |
cursor.Draw(spriteBatch); | |
spriteBatch.End(); | |
base.Draw(gameTime); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Graphics; | |
using Microsoft.Xna.Framework.Input; | |
namespace The_Calling | |
{ | |
class Player :IHuman | |
{ | |
string name; | |
int totalHealth; | |
int health; | |
int sanity; | |
float disposition; | |
Texture2D texture; | |
public Vector2 position = new Vector2(MainCalling.screenHeight*0.5f, MainCalling.screenHeight*0.5f); | |
Vector2 direction; | |
Vector2 mouse; | |
float rotation; | |
float speed = 8f; | |
public Player(string name, int totalHealth, int sanity, Texture2D texture) | |
{ | |
this.name = name; | |
this.totalHealth = totalHealth; | |
this.sanity = sanity; | |
this.texture = texture; | |
} | |
public Player(string name, int totalHealth, int health, int sanity, Texture2D texture) | |
{ | |
this.name = name; | |
this.totalHealth = totalHealth; | |
this.health = health; | |
this.sanity = sanity; | |
this.texture = texture; | |
} | |
public string GetName() | |
{ | |
return name; | |
} | |
public int GetHealth() | |
{ | |
return health; | |
} | |
public int GetSanity() | |
{ | |
return sanity; | |
} | |
public float GetDisposition(string name) | |
{ | |
//This won't be used in the Player's case, but is required | |
//by the IHuman interface. | |
//I'm not sure how I will want to handle disposition, | |
//I will probably just have this default to 100 | |
throw new NotImplementedException(); | |
} | |
public Array Items() | |
{ | |
throw new NotImplementedException(); | |
} | |
public void Update(Cursor cursor) | |
{ | |
KeyboardState keyboard = Keyboard.GetState(); | |
direction = Vector2.Zero; | |
mouse = cursor.mousePosition; | |
float mouseX = position.X - mouse.X; | |
float mouseY = position.Y - mouse.Y; | |
rotation = (float)Math.Atan2(mouseY, mouseX); | |
if (keyboard.IsKeyDown(Keys.W) || | |
keyboard.IsKeyDown(Keys.Up)) | |
{ | |
direction.Y = -1; | |
Move(); | |
} | |
if (keyboard.IsKeyDown(Keys.S) || | |
keyboard.IsKeyDown(Keys.Down)) | |
{ | |
direction.Y = 1; | |
Move(); | |
} | |
if (keyboard.IsKeyDown(Keys.A) || | |
keyboard.IsKeyDown(Keys.Left)) | |
{ | |
direction.X = -1; | |
Move(); | |
} | |
if (keyboard.IsKeyDown(Keys.D) || | |
keyboard.IsKeyDown(Keys.Right)) | |
{ | |
direction.X = 1; | |
Move(); | |
} | |
} | |
public void Move() | |
{ | |
direction.Normalize(); | |
position += direction * speed; | |
} | |
public void Draw(SpriteBatch spriteBatch) | |
{ | |
Vector2 origin = new Vector2(texture.Width / 2, texture.Height / 2); | |
spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, 1, SpriteEffects.None, 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment