Skip to content

Instantly share code, notes, and snippets.

@kb10uy
Created January 4, 2013 09:47
Show Gist options
  • Save kb10uy/4451300 to your computer and use it in GitHub Desktop.
Save kb10uy/4451300 to your computer and use it in GitHub Desktop.
いろいろ保安装置とか再現できそうな奴らをつくってみた。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using TrainSystemSimulator;
namespace TrainSystemSimulator.Simulate
{
/// <summary>
/// 列車が地上子を通過した時に発生するイベントのデリゲート型。
/// </summary>
public delegate void BeaconEventHandler();
/// <summary>
/// マスコンの状態。
/// </summary>
public enum MasterControl
{
EB = 0,
B8,
B7,
B6,
B5,
B4,
B3,
B2,
B1,
BF,
N,
P1,
P2,
P3,
P4,
P5,
}
/// <summary>
/// 保安装置スクリプトで実装する必要のあるインターフェース。
/// </summary>
public interface IProtectiveDevice
{
void Install();
void Initialize();
void Draw(SpriteBatch sb);
void Update(); //Trainクラスの何かを送る
//BeaconEventHandler
void BeaconPassed(BeaconEventArgs e);
}
/// <summary>
/// そのまま
/// </summary>
public class BeaconEventArgs : EventArgs
{
public BeaconEventArgs()
: base()
{
PassedBeacon = new Beacon();
}
public BeaconEventArgs(Beacon b)
{
PassedBeacon = b;
}
public Beacon PassedBeacon { get; private set; }
}
/// <summary>
/// 地上子
/// </summary>
public class Beacon
{
public int Position { get; set; }
public string Type { get; set; }
public ExternalSignal LinkedSingal { get; set; }
public void Update(List<double> tp, List<double> ptp,List<Train> train)
{
for (int i = 0; i < tp.Count; i++)
{
if (ptp[i] <= Position && tp[i] > Position)
{
train[i].ProtectiveDevice.BeaconPassed(new BeaconEventArgs(this));
}
}
}
}
/// <summary>
/// 列車を模した
/// </summary>
public class Train
{
public double Speed { get; protected set; }
public MasterControl Control { get; protected set; }
public IProtectiveDevice ProtectiveDevice { get; protected set; }
public PythonScript Script { get; protected set; }
public Train(PythonScript script)
{
Speed = 0.0;
Control = MasterControl.N;
Script = script;
ProtectiveDevice = Script.GetInstance<IProtectiveDevice>("ProtectiveDevice()");
ProtectiveDevice.Install();
ProtectiveDevice.Initialize();
}
public void Draw(SpriteBatch sb)
{
ProtectiveDevice.Draw(sb);
}
public void Update()
{
ProtectiveDevice.Update();
}
}
/// <summary>
/// 信号の現示
/// </summary>
public enum SignalKind
{
R,
YY,
Y,
YG,
YGF,
G,
GG
}
/// <summary>
/// 信号(まあ正確には車外なのでExternal)
/// </summary>
public class ExternalSignal
{
public SignalKind Kind { get; set; }
public ExternalSignal()
{
Kind = SignalKind.R;
}
}
public class Railway
{
public List<double> TrainPosition { get; private set; }
public List<double> PreviousTrainPosition { get; set; }
public List<Train> Trains { get; private set; }
public Dictionary<int, List<Beacon>> Beacons { get; private set; }
public Dictionary<int, ExternalSignal> Signals { get; private set; }
public Dictionary<int, string> SignalLinkBeacons { get; private set; }
public Railway()
{
TrainPosition = new List<double>();
PreviousTrainPosition = new List<double>();
Trains = new List<Train>();
Beacons = new Dictionary<int, List<Beacon>>();
Signals = new Dictionary<int, ExternalSignal>();
SignalLinkBeacons = new Dictionary<int, string>();
}
public void PutTrain(Train train, double pos = 0)
{
Trains.Add(train);
TrainPosition.Add(pos);
PreviousTrainPosition.Add(0);
}
public void RegistSignal(ExternalSignal sig, int pos)
{
Signals[pos] = sig;
}
public void RegistSignalLinkBeacon(string type, int pos)
{
SignalLinkBeacons[pos] = type;
}
public void RegistBeacon(string type, int pos)
{
if (Beacons[pos] != null)
{
//モウすでにそこに1個以上あった場合
Beacons[pos].Add(new Beacon { Type = type, LinkedSingal = null, Position = pos });
}
else
{
//まだそこに何もなかった
Beacons[pos] = new List<Beacon>();
Beacons[pos].Add(new Beacon { Type = type, LinkedSingal = null, Position = pos });
}
}
public void Update()
{
//地上子更新
foreach (var bs in Beacons)
{
foreach (Beacon b in bs.Value)
{
b.Update(TrainPosition,PreviousTrainPosition,Trains);
}
}
//あとは
/*
* 列車をすすめることとか色々
*/
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment