【发布时间】:2020-06-09 18:11:34
【问题描述】:
我正在尝试检测 Player obj 与 C# 中 objectList 列表中的任何其他 PhisicalEntity 之间的冲突,但我无法让它工作。有什么想法吗?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyGame
{
public partial class GameCanvas : Form
{
public int BlockSize = 10;
public Timer timer1;
private static PlayerInputHandler UINPUT = new PlayerInputHandler();
private static List<ObjContainer> playerList = new List<ObjContainer>();
private static List<ObjContainer> objectList = new List<ObjContainer>();
struct speedVec
{
public int NormalizeSpeedVector(int value)
{
return (value == 0) ? 0 : ((value < 0) ? -1 : 1);
}
}
class PlayerInputHandler
{
public int x = 0;
public int y = 0;
public PlayerInputHandler()
{
}
public void SenceUp()
{
this.y = (new speedVec()).NormalizeSpeedVector(this.y - 1);
}
public void SenceDown()
{
this.y = (new speedVec()).NormalizeSpeedVector(this.y + 1);
}
public void SenceRight()
{
this.x = (new speedVec()).NormalizeSpeedVector(this.x + 1);
}
public void SenceLeft()
{
this.x = (new speedVec()).NormalizeSpeedVector(this.x - 1);
}
public void SenceNull()
{
this.x = 0;
this.y = 0;
}
}
class ObjContainer
{
private int OID;
public PhisicalEntity OBJ;
public ObjContainer(int OID, PhisicalEntity OBJ)
{
this.OID = OID;
this.OBJ = OBJ;
}
}
abstract class PhisicalEntity
{
public static PictureBox OnScreenRepresentation;
public abstract void Step();
public int GetX()
{
return OnScreenRepresentation.Location.X;
}
public int GetY()
{
return OnScreenRepresentation.Location.Y;
}
}
class Player : PhisicalEntity
{
public int x;
public int y;
public int xs;
public int ys;
public int health;
public int gravity;
public int friction;
public Player(int x, int y, int xs, int ys, int health, int gravity, int friction, PictureBox repr)
{
this.x = x;
this.y = y;
this.xs = xs;
this.ys = ys;
this.health = health;
this.gravity = gravity;
this.friction = friction;
OnScreenRepresentation = repr;
}
override public void Step()
{
if (CheckColision(this.x, this.y, this.xs, this.ys))
{
this.xs = UINPUT.x;
this.ys = UINPUT.y;
UINPUT.SenceNull();
this.x += this.xs * 3;
this.y += this.ys * 3;
OnScreenRepresentation.Location = new Point(this.x, this.y);
}
}
public bool isBetwen(int value, int min, int max)
{
return (value < max) ? ((value > min) ? true : false) : false;
}
private bool CheckColision(int x, int y, int xs, int ys)
{
foreach (ObjContainer phisicalEmu in objectList)
{
PhisicalEntity obj = phisicalEmu.OBJ;
int phiX = obj.GetX();
int phiY = obj.GetY();
if (isBetwen(x + xs, phiX + 10, phiX) || isBetwen(x + 10 + xs, phiX + 10, phiX))
{
return true;
}
if (isBetwen(y + ys, phiY + 10, phiY) || isBetwen(y + 10 + ys, phiY + 10, phiY))
{
return true;
}
}
return false;
}
}
class Structural : PhisicalEntity
{
public int x;
public int y;
public int xs;
public int ys;
public int health;
public int gravity;
public int friction;
public PictureBox OnScreenRepresentation;
public Structural(int x, int y, int xs, int ys, int health, int gravity, int friction, PictureBox repr)
{
this.x = x;
this.y = y;
this.xs = xs;
this.ys = ys;
this.health = health;
this.gravity = gravity;
this.friction = friction;
this.OnScreenRepresentation = repr;
}
override public void Step()
{
this.xs = UINPUT.x;
this.ys = UINPUT.y;
UINPUT.SenceNull();
this.x += this.xs * 3;
this.y += this.ys * 3;
OnScreenRepresentation.Location = new Point(this.x, this.y);
}
}
public void InitTimer()
{
Timer timer = new Timer();
timer.Tick += new EventHandler(Step);
timer.Interval = 20;
timer.Start();
}
public void Step(object sender, EventArgs e)
{
foreach (ObjContainer obj in playerList)
{
obj.OBJ.Step();
}
}
public GameCanvas()
{
InitializeComponent();
}
private void GC_onLoad(object sender, EventArgs e)
{
RegisterNewPlayer(new Player(0, 0, 0, 0, 100, 3, 0, o_934), (new Random()).Next(((new Random()).Next())));
RegisterNewStruc(new Structural(0, 0, 0, 0, 0, 0, 0, o_980), (new Random()).Next(((new Random()).Next())));
InitTimer();
}
private void RegisterNewPlayer(Player obj, int KeyGen)
{
int num = ((new DateTime()).Year + (new DateTime()).Month + (new DateTime()).Day + (new DateTime()).Hour + (new DateTime()).Second + (new DateTime()).Millisecond) * KeyGen;
ObjContainer OnRegst = new ObjContainer(num, obj);
playerList.Add(OnRegst);
}
private void RegisterNewStruc(Structural obj, int KeyGen)
{
int num = ((new DateTime()).Year + (new DateTime()).Month + (new DateTime()).Day + (new DateTime()).Hour + (new DateTime()).Second + (new DateTime()).Millisecond) * KeyGen;
ObjContainer OnRegst = new ObjContainer(num, obj);
objectList.Add(OnRegst);
}
private void ProccessKeyEvents(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Up)
{
UINPUT.SenceUp();
}
if (e.KeyData == Keys.Down)
{
UINPUT.SenceDown();
}
if (e.KeyData == Keys.Left)
{
UINPUT.SenceLeft();
}
if (e.KeyData == Keys.Right)
{
UINPUT.SenceRight();
}
}
}
}
【问题讨论】: