PotatoPlan/Game1/Sources/Controlls/Controller.cs

101 lines
2.9 KiB
C#
Raw Normal View History

2020-04-07 17:50:31 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
2020-05-03 13:05:05 +02:00
using System;
2020-04-07 17:50:31 +02:00
2020-05-03 13:05:05 +02:00
class Controller
2020-04-07 17:50:31 +02:00
{
private KeyboardState state = Keyboard.GetState();
private GraphicsDeviceManager graphics;
2020-05-03 13:05:05 +02:00
private bool heldUp = false;
2020-04-07 17:50:31 +02:00
2020-05-03 13:05:05 +02:00
public void init(GraphicsDeviceManager Graphics)
2020-04-07 17:50:31 +02:00
{
graphics = Graphics;
}
2020-05-03 13:05:05 +02:00
public Vector2 updateWindow(int tileSize, int Spacing, Vector2 Size)
2020-04-07 17:50:31 +02:00
{
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Keys.D) && Size.X < 100)
{
Size.X++;
graphics.PreferredBackBufferWidth = (tileSize + Spacing) * (int)Size.X - Spacing;
2020-04-07 17:50:31 +02:00
}
if (state.IsKeyDown(Keys.A) && Size.X > 2)
{
Size.X--;
graphics.PreferredBackBufferWidth = (tileSize + Spacing) * (int)Size.X - Spacing;
2020-04-07 17:50:31 +02:00
}
2020-05-03 13:05:05 +02:00
if (state.IsKeyDown(Keys.W) && Size.Y < 20)
2020-04-07 17:50:31 +02:00
{
Size.Y++;
2020-05-06 16:22:30 +02:00
graphics.PreferredBackBufferHeight = (tileSize + Spacing) * (int)Size.Y - Spacing + 300;
2020-04-07 17:50:31 +02:00
}
if (state.IsKeyDown(Keys.S) && Size.Y > 2)
{
Size.Y--;
2020-05-06 16:22:30 +02:00
graphics.PreferredBackBufferHeight = (tileSize + Spacing) * (int)Size.Y - Spacing + 300;
2020-04-07 17:50:31 +02:00
}
return Size;
}
2020-05-03 13:05:05 +02:00
public int controllTileSize(Vector2 Size, int tileSize)
2020-04-07 17:50:31 +02:00
{
if (Size.X * tileSize + 5 > GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width)
2020-04-07 17:50:31 +02:00
{
tileSize--;
}
if (Size.X * tileSize - 5 < GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width && tileSize < 56)
2020-04-07 17:50:31 +02:00
{
tileSize++;
}
return tileSize;
}
2020-05-03 13:05:05 +02:00
public int controllSpeed(int Speed)
2020-04-07 17:50:31 +02:00
{
2020-05-03 13:05:05 +02:00
KeyboardState state = Keyboard.GetState();
2020-05-07 01:55:52 +02:00
if (state.IsKeyDown(Keys.Right) && Speed < 350)
2020-05-03 13:05:05 +02:00
{
Speed++;
}
2020-04-07 17:50:31 +02:00
2020-05-03 13:05:05 +02:00
if (state.IsKeyDown(Keys.Left) && Speed > 0)
{
Speed--;
}
return Speed;
2020-04-07 17:50:31 +02:00
}
2020-05-03 13:05:05 +02:00
public tractorPositionCorrector controllTractorSpeed(float tractorSpeed, Vector2 Position)
2020-04-07 17:50:31 +02:00
{
2020-05-03 13:05:05 +02:00
KeyboardState state = Keyboard.GetState();
tractorPositionCorrector Corrector = new tractorPositionCorrector(Position, tractorSpeed);
if (!heldUp)
{
if (state.IsKeyDown(Keys.Up) && tractorSpeed < 1)
{
Corrector.setTractorSpeed(tractorSpeed * 2);
heldUp = true;
}
else if (state.IsKeyDown(Keys.Down) && tractorSpeed > 0.0009765625)
{
Corrector.setTractorSpeed(tractorSpeed / 2);
heldUp = true;
}
}
else if (heldUp && !(state.IsKeyDown(Keys.Down) || state.IsKeyDown(Keys.Up)))
{
heldUp = false;
Corrector.setPosition((float)Math.Ceiling(Position.X), (float)Math.Ceiling(Position.Y));
}
return Corrector;
2020-04-07 17:50:31 +02:00
}
}