1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-26 02:09:24 +01:00
MLEM/Sandbox/GameImpl.cs

92 lines
3.6 KiB
C#
Raw Normal View History

2019-11-08 15:35:15 +01:00
using System;
2019-09-19 20:23:18 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2019-11-08 15:35:15 +01:00
using Microsoft.Xna.Framework.Input;
2019-11-10 22:36:25 +01:00
using MLEM.Cameras;
using MLEM.Extended.Extensions;
using MLEM.Extended.Tiled;
2019-11-08 15:35:15 +01:00
using MLEM.Extensions;
using MLEM.Font;
2019-09-19 20:23:18 +02:00
using MLEM.Startup;
using MLEM.Textures;
using MLEM.Ui;
using MLEM.Ui.Elements;
using MLEM.Ui.Style;
using MonoGame.Extended;
2019-11-10 22:36:25 +01:00
using MonoGame.Extended.Tiled;
2019-09-19 20:23:18 +02:00
namespace Sandbox {
public class GameImpl : MlemGame {
2019-11-10 22:36:25 +01:00
private Camera camera;
private TiledMap map;
private IndividualTiledMapRenderer mapRenderer;
private TiledMapCollisions collisions;
2019-11-10 22:36:25 +01:00
2019-09-19 20:23:18 +02:00
public GameImpl() {
this.IsMouseVisible = true;
2019-11-08 15:35:15 +01:00
this.Window.ClientSizeChanged += (o, args) => {
Console.WriteLine("Size changed");
};
2019-09-19 20:23:18 +02:00
}
protected override void LoadContent() {
base.LoadContent();
2019-11-10 22:36:25 +01:00
this.map = LoadContent<TiledMap>("Tiled/Map");
this.mapRenderer = new IndividualTiledMapRenderer(this.map);
this.collisions = new TiledMapCollisions(this.map);
2019-11-10 22:36:25 +01:00
this.camera = new Camera(this.GraphicsDevice) {
AutoScaleWithScreen = true,
Scale = 2,
LookingPosition = new Vector2(25, 25) * this.map.GetTileSize(),
MinScale = 0.25F,
MaxScale = 4
};
/*var tex = LoadContent<Texture2D>("Textures/Test");
this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) {
Font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")),
TextScale = 0.1F,
PanelTexture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8),
ButtonTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4)
};
this.UiSystem.AutoScaleReferenceSize = new Point(1280, 720);
this.UiSystem.AutoScaleWithScreen = true;
this.UiSystem.GlobalScale = 5;
2019-11-05 21:44:51 +01:00
var panel = new Panel(Anchor.Center, new Vector2(0, 100), Vector2.Zero) {SetWidthBasedOnChildren = true};
panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(100, 10)));
panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10)));
2019-11-10 22:36:25 +01:00
this.UiSystem.Add("Panel", panel);*/
2019-09-19 20:23:18 +02:00
}
2019-12-05 17:35:24 +01:00
protected override void DoUpdate(GameTime gameTime) {
base.DoUpdate(gameTime);
2019-11-08 15:35:15 +01:00
if (this.InputHandler.IsKeyPressed(Keys.F11))
this.GraphicsDeviceManager.SetFullscreen(!this.GraphicsDeviceManager.IsFullScreen);
2019-11-10 22:36:25 +01:00
var delta = this.InputHandler.ScrollWheel - this.InputHandler.LastScrollWheel;
if (delta != 0) {
this.camera.Zoom(0.1F * Math.Sign(delta), this.InputHandler.MousePosition.ToVector2());
}
2019-11-08 15:35:15 +01:00
}
2019-09-19 20:23:18 +02:00
protected override void DoDraw(GameTime gameTime) {
this.GraphicsDevice.Clear(Color.Black);
2019-11-10 22:36:25 +01:00
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, this.camera.ViewMatrix);
this.mapRenderer.Draw(this.SpriteBatch, this.camera.GetVisibleRectangle().ToExtended());
foreach (var tile in this.collisions.GetCollidingTiles(new RectangleF(0, 0, this.map.Width, this.map.Height))) {
foreach (var area in tile.Collisions) {
this.SpriteBatch.DrawRectangle(area.Position * this.map.GetTileSize(), area.Size * this.map.GetTileSize(), Color.Red);
}
}
2019-11-10 22:36:25 +01:00
this.SpriteBatch.End();
2019-09-19 20:23:18 +02:00
base.DoDraw(gameTime);
}
}
}