mirror of
https://github.com/eliasstepanik/2DEngine.git
synced 2026-01-10 04:28:28 +00:00
Small Game with collisions
This commit is contained in:
parent
526c9a3973
commit
61e4e3d642
@ -14,44 +14,95 @@ public class Game : Window
|
||||
}
|
||||
|
||||
Vector2 ballPosition = new Vector2(100, 100);
|
||||
UiText _text = new UiText(new Vector2(0,0), "Hello World!", 20, Color.RED);
|
||||
|
||||
UiText playerDebugText = new UiText(new Vector2(0,0), "Hello World!", 20, Color.RED);
|
||||
UiText playerInputText = new UiText(new Vector2(0,30), "Hello World!", 20, Color.RED);
|
||||
UiText playerVelocityText= new UiText(new Vector2(0,60), "Hello World!", 20, Color.RED);
|
||||
|
||||
|
||||
Circle circle = new Circle(new Vector2(0,0), 20, Color.RED);
|
||||
Circle circleD = new Circle(new Vector2(0,0), 2, Color.RED);
|
||||
Rectangle _cube = new Rectangle(new Vector2(100,100), new Vector2(10,20), Color.BLUE);
|
||||
|
||||
private List<Rectangle> _cubes;
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
ballPosition = new Vector2((float) Width / 2, (float) Height / 2);
|
||||
circle.Position = ballPosition;
|
||||
RegisterGameObject(circle);
|
||||
RegisterUiObject(_text);
|
||||
RegisterUiObject(playerDebugText);
|
||||
RegisterUiObject(playerInputText);
|
||||
RegisterUiObject(playerVelocityText);
|
||||
RegisterGameObject(_cube);
|
||||
RegisterGameObject(circleD);
|
||||
|
||||
_cubes = new List<Rectangle>()
|
||||
{
|
||||
new Rectangle(new Vector2(0,0), new Vector2(Width,20), Color.BLUE),
|
||||
new Rectangle(new Vector2(0,0), new Vector2(20,Height), Color.BLUE),
|
||||
new Rectangle(new Vector2(Width - 20 ,0), new Vector2(20,Height), Color.BLUE),
|
||||
new Rectangle(new Vector2(0,Height - 20), new Vector2(Width,20), Color.BLUE)
|
||||
};
|
||||
|
||||
foreach (var VARIABLE in _cubes)
|
||||
{
|
||||
RegisterGameObject(VARIABLE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
base.Start();
|
||||
}
|
||||
|
||||
Vector2 playerVelocity = new Vector2(0,0);
|
||||
Vector2 playerInput = new Vector2(0,0);
|
||||
float speed = 2;
|
||||
|
||||
bool _collided = false;
|
||||
public override void Update()
|
||||
{
|
||||
base.Update();
|
||||
if (!circle.Intersects(_cube))
|
||||
playerInputText.Text = "Player Input: " + playerInput;
|
||||
playerVelocityText.Text = "Player Velocity: " + playerVelocity;
|
||||
playerDebugText.Text = "Player Debug: " + circle.Position;
|
||||
|
||||
foreach (var rectangle in _cubes)
|
||||
{
|
||||
|
||||
if (circle.Intersects(rectangle))
|
||||
{
|
||||
playerVelocity = new Vector2(0,0);
|
||||
playerInput = new Vector2(0,0);
|
||||
_collided = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_collided = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (Raylib.IsKeyDown(KeyboardKey.KEY_D)) ballPosition.X += 2.0f;
|
||||
if (Raylib.IsKeyDown(KeyboardKey.KEY_A)) ballPosition.X -= 2.0f;
|
||||
if (Raylib.IsKeyDown(KeyboardKey.KEY_W)) ballPosition.Y -= 2.0f;
|
||||
if (Raylib.IsKeyDown(KeyboardKey.KEY_S)) ballPosition.Y += 2.0f;
|
||||
|
||||
double dir = _cube.DirectionTo(circle);
|
||||
Vector2 dirVec = new Vector2((float) Math.Cos(dir), (float) Math.Sin(dir));
|
||||
if (Raylib.IsKeyDown(KeyboardKey.KEY_D)) playerInput.X = Raymath.Lerp(playerInput.X, 1, 0.5f * Raylib.GetFrameTime() * 10);
|
||||
if (Raylib.IsKeyDown(KeyboardKey.KEY_A)) playerInput.X = Raymath.Lerp(playerInput.X, -1, 0.5f * Raylib.GetFrameTime() * 10);
|
||||
if (Raylib.IsKeyDown(KeyboardKey.KEY_W)) playerInput.Y = Raymath.Lerp(playerInput.Y, -1, 0.5f * Raylib.GetFrameTime() * 10);
|
||||
if (Raylib.IsKeyDown(KeyboardKey.KEY_S)) playerInput.Y = Raymath.Lerp(playerInput.Y, 1, 0.5f * Raylib.GetFrameTime() * 10);
|
||||
|
||||
|
||||
|
||||
playerVelocity += playerInput;
|
||||
|
||||
circle.Position += playerVelocity * Raylib.GetFrameTime() * speed;
|
||||
|
||||
circleD.Position = circle.Position + dirVec * circle.Radius;
|
||||
|
||||
circle.Position = ballPosition;
|
||||
Camera.target = new Vector2(0, 0);
|
||||
Console.Clear();
|
||||
Console.WriteLine(@"BallPisiton: {0}",circle.Position);
|
||||
Console.WriteLine(@"BallDirection: {0}",circleD.Position);
|
||||
}
|
||||
|
||||
float Lerp(float start, float end, float amount)
|
||||
{
|
||||
float result = start + amount*(end - start);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -44,20 +44,5 @@ public class Rectangle : GameObject
|
||||
return Math.Tan((position.Y / position.X));
|
||||
|
||||
}
|
||||
|
||||
public double DirectionTo(Circle other)
|
||||
{
|
||||
Vector2 otherPosition = other.Position;
|
||||
Vector2 position = Position;
|
||||
|
||||
|
||||
//α = arccos[(xa * xb + ya * yb) / (√(xa2 + ya2) * √(xb2 + yb2))]
|
||||
|
||||
|
||||
return Math.Atan((position.X * otherPosition.X + position.Y * otherPosition.Y) /
|
||||
(Math.Sqrt(Math.Pow(position.X, 2) + Math.Pow(position.Y, 2)) *
|
||||
Math.Sqrt(Math.Pow(other.Position.X, 2) + Math.Pow(other.Position.Y, 2))));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -34,18 +34,18 @@ public class Window
|
||||
|
||||
public virtual void Update()
|
||||
{
|
||||
Parallel.ForEach(gameObjects, gameObject =>
|
||||
foreach (var gameObject in gameObjects)
|
||||
{
|
||||
gameObject.Draw();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void UpdateUi()
|
||||
{
|
||||
Parallel.ForEach(uiObjects, uiObject =>
|
||||
foreach (var uiObject in uiObjects)
|
||||
{
|
||||
uiObject.Draw();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void RegisterGameObject(GameObject gameObject)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user