tunnet/Game1.cs
2025-06-22 13:48:35 +03:00

140 lines
4 KiB
C#

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGameLibrary;
using MonoGameLibrary.Graphics;
// using tunnet.World;
namespace tunnet;
public class Game1 : Core
{
// private World.Map _world;
// private Texture2D _logo;
public Game1() : base("Tunnet but not", 1280, 720, fullScreen: false)
{
// Content.RootDirectory = "Content";
// IsMouseVisible = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
// _world = new World.Map(mapId: 0);
// _world.LoadMap();
//
base.Initialize();
}
private Sprite _404;
// private AnimatedSprite _animatedSprite;
protected override void LoadContent()
{
TextureAtlas atlas = TextureAtlas.FromFile(Content, "images/atlas-definition.xml");
_404 = atlas.CreateSprite("404");
// _animatedSprite = atlas.CreateAnimatedSprite("404");
}
private const int _CAMERA_SPEED = 5;
Viewport _cameraPosition;
Point _mousePosition;
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
// Animations
// _animatedSprite.Update(gameTime);
// Inputs
// https://docs.monogame.net/articles/tutorials/building_2d_games/10_handling_input/
if (_cameraPosition.Equals(new Viewport())) _cameraPosition = GraphicsDevice.Viewport;
CheckKeyboardInput();
CheckMouseInput();
base.Update(gameTime);
}
private void CheckKeyboardInput()
{
// Get the state of keyboard input
KeyboardState keyboardState = Keyboard.GetState();
// If the space key is held down, the movement speed increases by 1.5
int speed = _CAMERA_SPEED;
if (keyboardState.IsKeyDown(Keys.Space))
{
speed = (int)Math.Floor(speed * 1.5);
}
if (keyboardState.IsKeyDown(Keys.W) || keyboardState.IsKeyDown(Keys.Up))
{
_cameraPosition.Y += speed;
}
if (keyboardState.IsKeyDown(Keys.S) || keyboardState.IsKeyDown(Keys.Down))
{
_cameraPosition.Y -= speed;
}
if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left))
{
_cameraPosition.X += speed;
}
if (keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right))
{
_cameraPosition.X -= speed;
}
//
GraphicsDevice.Viewport = _cameraPosition;
}
private void CheckMouseInput()
{
// Get the state of mouse input
MouseState mouseState = Mouse.GetState();
//Move camera with right mouse button
if (mouseState.RightButton == ButtonState.Pressed)
{
_mousePosition -= mouseState.Position; // delta
_cameraPosition!.X = _cameraPosition.X - _mousePosition.X;
_cameraPosition.Y = _cameraPosition.Y - _mousePosition.Y;
}
//
GraphicsDevice.Viewport = _cameraPosition;
_mousePosition = mouseState.Position; // here because of wasd breaking it
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
SpriteBatch.Begin(sortMode: SpriteSortMode.Deferred);
// World.Map.Draw draw = new World.Map.Draw(in _spriteBatch); //TODO: fix
// _spriteBatch.Draw(_world._defaultTile, _world._defaultTile.Bounds.Center.ToVector2(), Color.White); //TODO: fix
/// TODO: Draw
/// 1. floor
/// 2. wires
/// 3. buildings
/// 4. entities
/// 5. walls
///
/// MINIMIZE texture swaps
/// MAXIMIZE drawRectangle usage per texture
_404.Draw(SpriteBatch, Vector2.Zero);
SpriteBatch.End();
//
base.Draw(gameTime);
}
}