概要
cscの作法、調べてみた。
bulletmllib、見つけたので、使ってみた。
練習問題、やってみた。
練習問題
弾幕sandboxを書け。
写真
サンプルコード
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using Tamago;
public class TestManager: BulletManager {
private float _x,
_y,
_rand,
_rank;
public const float TestRand = 0.1337f;
public const float TestRank = 0.3246f;
public TestManager(): base() {
_rand = TestRand;
_rank = TestRank;
}
public override float PlayerX {
get {
return _x;
}
}
public override float PlayerY {
get {
return _y;
}
}
public override float Rand {
get {
return _rand;
}
}
public override float Rank {
get {
return _rank;
}
}
public new List<Bullet> Bullets {
get {
return base.Bullets;
}
}
public void SetPlayerPosition(float x, float y) {
_x = x;
_y = y;
}
public void SetRand(float rand) {
_rand = rand;
}
public void SetRank(float rank) {
_rank = rank;
}
public override void Update() {
for (int i = Bullets.Count - 1; i >= 0; i--)
{
var b = Bullets[i];
if (!b.IsVanished)
b.Update();
}
}
}
public class Stg {
public TestManager TestManager;
BulletPattern DummyPattern;
public void SetUp() {
TestManager = new TestManager();
DummyPattern = new BulletPattern(@"<bulletml/>");
}
public Bullet CreateTopLevelBullet(string xml, string name = "top") {
var bullet = TestManager.CreateBullet();
var pattern = new BulletPattern(xml);
bullet.SetPattern(pattern.CopyAction(name), isTopLevel: true);
return bullet;
}
public void Update() {
TestManager.Update();
}
}
public partial class Form1: Form {
TextBox bo1;
Stg stg;
Point start;
Pen pen;
float r;
double sin;
double cos;
float paramX;
float paramY;
float radius;
int parameter;
public Form1() {
Text = "弾幕sandbox";
ClientSize = new Size(400, 600);
bo1 = new TextBox();
bo1.Location = new Point(20, 360);
bo1.Size = new Size(370, 230);
bo1.Multiline = true;
bo1.ScrollBars = ScrollBars.Both;
bo1.Text = @"
<bulletml>
<action label=""top"">
<repeat>
<times>222</times>
<action>
<fire>
<direction type=""sequence"">23</direction>
<bullet>
<action>
<wait>20 + $rand * 50</wait>
<changeDirection>
<direction type=""aim"">0</direction>
<term>10</term>
</changeDirection>
</action>
</bullet>
</fire>
<wait>1</wait>
</action>
</repeat>
</action>
</bulletml>
";
Controls.AddRange(new Control[] {
bo1
});
Button btn1 = new Button();
btn1.Location = new Point(300, 330);
btn1.Text = "撃て";
btn1.Click += btn1_Click;
Controls.AddRange(new Control[] {
btn1
});
start = new Point(200, 200);
pen = new Pen(Color.Black, 1);
r = 8;
sin = Math.Sin(parameter * (Math.PI / 180));
cos = Math.Cos(parameter * (Math.PI / 180));
paramX = (float)(cos * radius) + start.X;
paramY = (float)(sin * radius) + start.Y;
radius = 100;
parameter = 0;
stg = new Stg();
stg.SetUp();
Timer timer = new Timer();
timer.Interval = 16;
timer.Tick += new EventHandler(Update);
timer.Start();
}
private void Update(object sender, EventArgs e) {
Invalidate();
parameter += 4;
if (parameter == 360)
{
parameter = 0;
}
sin = Math.Sin(parameter * (Math.PI / 180));
cos = Math.Cos(parameter * (Math.PI / 180));
paramX = (float)(cos * radius) + start.X;
paramY = (float)(sin * radius) + start.Y;
stg.Update();
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.DrawEllipse(pen, paramX, paramY, r, r);
stg.TestManager.Bullets.ForEach(b => {
g.DrawEllipse(pen, 200 + b.X, 200 + b.Y, 5, 5);
});
}
void btn1_Click(object sender, System.EventArgs e) {
var str = bo1.Text;
stg.CreateTopLevelBullet(str);
}
[STAThread]
public static void Main() {
Application.Run(new Form1());
}
}
以上。
