public class Game1 : Microsoft.Xna.Framework.Game
{
// メンバー変数の宣言
GraphicsDeviceManager graphics;
private Renderer renderer;
private Chara chara;
private InputState input;
int[,] map = new int[16, 24]
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
/// <summary>
/// コンストラクタ
/// </summary>
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = Screen.Width;
graphics.PreferredBackBufferHeight = Screen.Height;
}
/// <summary>
/// 初期化
/// </summary>
protected override void Initialize()
{
base.Window.Title = "アクション";
// インスタンスの生成
renderer = new Renderer(Content, GraphicsDevice);
input = new InputState();
chara = new Chara(input);
chara.Initialize();
base.Initialize();
}
/// <summary>
/// 読み込み
/// </summary>
protected override void LoadContent()
{
renderer.LoadTexture("stage");
renderer.LoadTexture("map");
renderer.LoadTexture("chara");
}
/// <summary>
/// 解放
/// </summary>
protected override void UnloadContent()
{
renderer.Unload();
}
/// <summary>
/// 更新
/// </summary>
/// <param name="gameTime"></param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed
|| Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
input.Update();
chara.Update();
base.Update(gameTime);
}
/// <summary>
/// 表示
/// </summary>
/// <param name="gameTime"></param>
protected override void Draw(GameTime gameTime)
{
renderer.Begin();
renderer.DrawTexture("stage", new Vector2(0, 0));
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < 16; j++)
{
renderer.DrawTexture("map", new Vector2(i * 32, j * 32), new Vector2(32, 32), new Vector2((float)map[j, i], 0));
}
}
chara.Draw(renderer);
renderer.End();
base.Draw(gameTime);
}
}
class Renderer
{
// メンバー変数の宣言
private ContentManager contentManager;
private SpriteBatch spriteBatch;
private GraphicsDevice graphicsDevice;
private Dictionary<string, Texture2D>
textures = new Dictionary<string, Texture2D>();
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="content"></param>
/// <param name="graphics"></param>
public Renderer(ContentManager content, GraphicsDevice graphics)
{
contentManager = content;
contentManager.RootDirectory = "Content";
graphicsDevice = graphics;
spriteBatch = new SpriteBatch(graphics);
}
/// <summary>
/// 読み込み
/// </summary>
/// <param name="name"></param>
public void LoadTexture(string name)
{
textures[name] = contentManager.Load<Texture2D>(name);
}
/// <summary>
/// 解放
/// </summary>
public void Unload()
{
textures.Clear();
contentManager.Unload();
}
/// <summary>
/// 描画の開始
/// </summary>
public void Begin()
{
spriteBatch.Begin();
}
/// <summary>
/// 描画の終了
/// </summary>
public void End()
{
spriteBatch.End();
}
/// <summary>
/// 表示
/// </summary>
/// <param name="name"></param>
/// <param name="position"></param>
public void DrawTexture(string name, Vector2 position)
{
spriteBatch.Draw(textures[name], position, Color.White);
}
/// <summary>
/// 表示
/// </summary>
/// <param name="name"></param>
/// <param name="position"></param>
/// <param name="bmp"></param>
public void DrawTexture(string name, Vector2 position, Vector2 size, Vector2 bmp)
{
spriteBatch.Draw(textures[name], position,
new Rectangle((int)bmp.X * (int)size.X, (int)bmp.Y * (int)size.Y, (int)size.X, (int)size.Y), Color.White);
}
}
class InputState
{
// メンバー変数の宣言
private Vector2 velocity = Vector2.Zero;
/// <summary>
/// コンストラクタ
/// </summary>
public InputState()
{
}
/// <summary>
/// 移動量の獲得
/// </summary>
/// <returns></returns>
public Vector2 Velocity
{
get
{
return velocity;
}
}
/// <summary>
/// 更新
/// </summary>
public void Update()
{
var keyState = Keyboard.GetState();
UpdateVelocity(keyState);
}
/// <summary>
/// 移動速度の更新
/// </summary>
public void UpdateVelocity(KeyboardState keyState)
{
velocity = Vector2.Zero;
if (keyState.IsKeyDown(Keys.Right))
{
velocity.X = 1.0f;
}
if (keyState.IsKeyDown(Keys.Left))
{
velocity.X = -1.0f;
}
if (keyState.IsKeyDown(Keys.Down))
{
velocity.Y = 1.0f;
}
if (keyState.IsKeyDown(Keys.Up))
{
velocity.Y = -1.0f;
}
if (velocity.Length() != 0)
{
velocity.Normalize();
}
}
}
class Chara : Actor
{
// メンバー変数の宣言
private InputState input;
/// <summary>
/// コンストラクタ
/// </summary>
public Chara(InputState input)
{
this.input = input;
Radius = 16;
Name = "chara";
}
/// <summary>
/// 初期化
/// </summary>
public override void Initialize()
{
Position = new Vector2(144, 464);
}
/// <summary>
/// 更新
/// </summary>
public override void Update()
{
Position += input.Velocity * 8;
var min = new Vector2(Radius, Radius);
var max = new Vector2(Screen.Width - Radius, Screen.Height - 48);
Position = Vector2.Clamp(Position, min, max);
}
}
abstract class Actor
{
// メンバー変数の宣言
protected Vector2 Position { get; set; }
protected int Radius { get; set; }
protected string Name { get; set; }
/// <summary>
/// コンストラクタ
/// </summary>
protected Actor()
{
}
/// <summary>
/// 初期化抽象メソッド
/// </summary>
public abstract void Initialize();
/// <summary>
/// 更新抽象メソッド
/// </summary>
public abstract void Update();
/// <summary>
/// 表示
/// </summary>
/// <param name="renderer"></param>
public void Draw(Renderer renderer)
{
renderer.DrawTexture(Name, Position - new Vector2(Radius, Radius));
}
}