STEP.02 キャラクタイベントのコマンド化
このチュートリアルの完成イメージ


概要・解説
前回はカーソルキーを押すことで上下左右にプレイヤー移動するようにしていました。早速ネットワークに入りたいところですが、少し回り道になりますがプレイヤーコマンドクラスを実装してサーバー処理をエミュレートする画面を作ってみたいと思います。
今回のチュートリアルで特に注目して欲しいポイントはSample2DRPG.Command名前空間に追加したPlayerCommand、AddPlayerCommand、RemPlayerCommand、UpdPlayerCommandの 4 つのクラスです。PlayerCommandから派生した 3 つのコマンドはそれぞれプレイヤーの追加、削除および行動を実行するクラスとなります。このオブジェクトをネットワーク経由できれば大勢でプレイヤーを動かすことが可能になります。
また、ServerEmulatorというWindowsFormも追加しているのですが、この画面を使いサーバーの代わりにコマンドを生成してイベントを発生させるところまで行っています。このクラスについては後々デバッグをするのに便利だと思い作ったので現時点で処理の詳細を見る必要はないと思います。余力のある方は覗いてみてください。
コマンド化に伴い前回のソースから所々変更されています。全てのソースはページ最下部のソリューションに含まれています。
注意事項
このチュートリアルは、REFMAP様が配布しているフリー画像素材を使用してます。
このソフト内で使用されている画像を、このゲームを遊ぶ以外の用途には使用しないで下さい。
※ 詳細は First Seed Material をご確認ください。
※ 詳細は First Seed Material をご確認ください。
ソリューション設定

参照設定に System.Windows.Forms を追加しています。
ソリューションエクスプローラーの参照設定を右クリックしてコンテキストメニューから
[参照の追加(R)...]を選択して参照を追加して下さい。

-
Content
- chara02.dds
- chara03.dds
- chara04.dds
- chara05.dds
キャラクタが全部で5種類に増えました。
-
Charactor
- CharaManager.cs
キャラクタ増加に伴い管理クラスを追加しました。
-
Command
- PlayerCommand.cs
- AddPlayerCommand.cs
- RemPlayerCommand.cs
- UpdPlayerCommand.cs
今回のチュートリアルで最も重要なクラスです。
-
Util
- ServerEmulator.cs
サーバー処理を代理してコマンドを発行します。
ソースコード
※ 重要なクラスをピックアップ
PlayerCommand.cs
using System;
using System.Collections.Generic;
using System.Text;
using Sample2DRPG.Charactor;
namespace Sample2DRPG.Command
{
/// <summary>
/// プレイヤーコマンド
/// </summary>
public interface IPlayerCommand
{
/// <summary>
/// コマンド実行
/// </summary>
/// <param name="dict"></param>
void Execute(Dictionary<int, Player> dict);
}
// プレイヤーコマンドデリゲート
public delegate void PlayerCommandDelegate(IPlayerCommand command);
}
AddPlayerCommand.cs
using System.Collections.Generic;
using System.Text;
using Sample2DRPG.Charactor;
namespace Sample2DRPG.Command
{
/// <summary>
/// プレイヤーコマンド
/// </summary>
public interface IPlayerCommand
{
/// <summary>
/// コマンド実行
/// </summary>
/// <param name="dict"></param>
void Execute(Dictionary<int, Player> dict);
}
// プレイヤーコマンドデリゲート
public delegate void PlayerCommandDelegate(IPlayerCommand command);
}
using System;
using System.Collections.Generic;
using System.Text;
using Sample2DRPG.Charactor;
namespace Sample2DRPG.Command
{
public class AddPlayerCommand : IPlayerCommand
{
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="id">識別番号</param>
/// <param name="name">名前</param>
/// <param name="type">タイプ</param>
public AddPlayerCommand(int id, string name, int type)
{
this.id = id;
this.name = name;
this.type = type;
}
/// <summary>
/// コマンド実行
/// </summary>
/// <param name="dict"></param>
public void Execute(Dictionary<int, Player> dict)
{
dict.Add(id, new Player(id, name, type));
}
// プレイヤー識別番号
int id;
// プレイヤー名称
string name;
// キャラクタタイプ
int type;
}
}
RemPlayerCommand.cs
using System.Collections.Generic;
using System.Text;
using Sample2DRPG.Charactor;
namespace Sample2DRPG.Command
{
public class AddPlayerCommand : IPlayerCommand
{
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="id">識別番号</param>
/// <param name="name">名前</param>
/// <param name="type">タイプ</param>
public AddPlayerCommand(int id, string name, int type)
{
this.id = id;
this.name = name;
this.type = type;
}
/// <summary>
/// コマンド実行
/// </summary>
/// <param name="dict"></param>
public void Execute(Dictionary<int, Player> dict)
{
dict.Add(id, new Player(id, name, type));
}
// プレイヤー識別番号
int id;
// プレイヤー名称
string name;
// キャラクタタイプ
int type;
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Sample2DRPG.Charactor;
namespace Sample2DRPG.Command
{
public class RemPlayerCommand : IPlayerCommand
{
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="id">識別番号</param>
public RemPlayerCommand(int id)
{
this.id = id;
}
/// <summary>
/// コマンド実行
/// </summary>
/// <param name="dict"></param>
public void Execute(Dictionary<int, Player> dict)
{
// 対象プレイヤーをリストから除外
dict.Remove(this.id);
}
// プレイヤー識別番号
int id;
}
}
UpdPlayerCommand.cs
using System.Collections.Generic;
using System.Text;
using Sample2DRPG.Charactor;
namespace Sample2DRPG.Command
{
public class RemPlayerCommand : IPlayerCommand
{
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="id">識別番号</param>
public RemPlayerCommand(int id)
{
this.id = id;
}
/// <summary>
/// コマンド実行
/// </summary>
/// <param name="dict"></param>
public void Execute(Dictionary<int, Player> dict)
{
// 対象プレイヤーをリストから除外
dict.Remove(this.id);
}
// プレイヤー識別番号
int id;
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Sample2DRPG.Charactor;
namespace Sample2DRPG.Command
{
public class UpdPlayerCommand : IPlayerCommand
{
public UpdPlayerCommand(int id, int action)
{
this.id = id;
this.action = action;
}
/// <summary>
/// コマンド実行
/// </summary>
/// <param name="dict"></param>
public void Execute(Dictionary<int, Player> dict)
{
// 対象プレイヤーを取得
Player player = dict[this.id];
// アクションを更新
player.doAction(this.action);
}
// プレイヤー識別番号
int id;
// アクション
int action;
}
}
using System.Collections.Generic;
using System.Text;
using Sample2DRPG.Charactor;
namespace Sample2DRPG.Command
{
public class UpdPlayerCommand : IPlayerCommand
{
public UpdPlayerCommand(int id, int action)
{
this.id = id;
this.action = action;
}
/// <summary>
/// コマンド実行
/// </summary>
/// <param name="dict"></param>
public void Execute(Dictionary<int, Player> dict)
{
// 対象プレイヤーを取得
Player player = dict[this.id];
// アクションを更新
player.doAction(this.action);
}
// プレイヤー識別番号
int id;
// アクション
int action;
}
}
ソリューションファイル
Download Sample2DRPG_STEP02.zip
更新日 2008/05/29