public SceneSystemDemo(Layer layer) : base(layer) { // Creating new scene. _testScene = SceneMgr.CreateScene("SceneDemoDummy"); var mainLayer = _testScene.CreateLayer("main"); var backgroundLayer = _testScene.CreateLayer("background"); // Update and Draw events will be executed for this layer first. // This can be counter-intuitive, but this will put the layer on the back. // Because it is being drawn first, everything else will be drawn on top of it. backgroundLayer.Priority = 999; // Applying a shader to the thingy. backgroundLayer.PostprocessorEffects.Add(ResourceHub.GetResource <Effect>("Effects", "Seizure")); // See ECDemo to learn how those work. new Player(mainLayer, new Vector2(400, 300)); // Player will not draw lines to these bots, because they are on a different layer. for (var i = 0; i < 10; i += 1) { var bot = new Bot(backgroundLayer); var position = bot.GetComponent <PositionComponent>(); position.Position = new Vector2(ECDemoFactory.Random.Next(100, 700), ECDemoFactory.Random.Next(100, 500)); } // Player will draw lines to these bots, because they are on the same layer. for (var i = 0; i < 5; i += 1) { var bot = new Bot(mainLayer); var position = bot.GetComponent <PositionComponent>(); position.Position = new Vector2(ECDemoFactory.Random.Next(100, 700), ECDemoFactory.Random.Next(100, 500)); } }
public Entity Make(Layer layer) { // Bot is a pure EC entity. This means, that it uses // non-derived Entity class with no logic in it. // But that poses a problem - where to assemble the entity? // For this purpose, entity templates exist. // You can assemble your entity here and then create it using // Entity.CreateFromTemplate(layer, "Bot"); // // Though, entity templates are not required. You can assemble new entities // anywhere you like. You can even add new components and remove existing // at any time. _botSprite = ResourceHub.GetResource <Sprite>("DefaultSprites", "Bot"); var entity = new Entity(layer); entity.AddComponent(new PositionComponent(Vector2.Zero)); entity.AddComponent(new ActorComponent(_botSprite)); var bot = new BotComponent(); // It is recommended to reuse random objects. bot.TurningSpeed = ECDemoFactory.Random.Next(120, 240); entity.AddComponent(bot); return(entity); }
public UtilsDemo(Layer layer) : base(layer) { _grayscale = ResourceHub.GetResource <Effect>("Effects", "Grayscale"); _fireSprite = ResourceHub.GetResource <Sprite>("DefaultSprites", "Fire"); // Animation. // Animation class is more sophisticated way of implementing animations. // It can be used for anything from sprite animation to UI element position. _fireAnimation = new Animation(); _fireAnimation.Speed = _animationSpeed; // You can set an easing to make animation non-linear. // If it's not set, animation will be linear. _fireAnimation.Easing = Easing.EaseInCirc; // You can trigger a method at the end of an animation. _fireAnimation.AnimationEndEvent += FireAnimationEnd; _fireAnimation.Start(true); // Animation. // Alarms. _slowTimeKeeper = new TimeKeeper(); // Slowing down time for this time keeper. _slowTimeKeeper.TimeMultiplier = 0.5f; _autoAlarm = new AutoAlarm(_alarmPeriod); _slowAlarm = new Alarm(); // This alarm will now use custom TimeKeeper, which is 2 times // slower than global TimeKeeper. _slowAlarm.TimeKeeper = _slowTimeKeeper; _slowAlarm.Set(_alarmPeriod); _slowAlarm.TriggerEvent += AlarmTrigger; // Alarms. // Camera. _camera = new Camera2D(400, 600); _camera.PortPosition = new Vector2(400, 0); _camera.BackgroundColor = Color.Black; _camera.PostprocessorEffects.Add(_grayscale); _camera.PostprocessingMode = PostprocessingMode.Camera; // Camera. _random = new RandomExt(); // State machine. // State machines are very useful for animation and complex logic. _stateMachine = new StateMachine <TestStates>(TestStates.Green, this); // Filling up the state machine with events. _stateMachine.AddState(TestStates.Green, Green, GreenEnter, GreenExit); _stateMachine.AddState(TestStates.Blue, Blue); _stateMachine.AddState(TestStates.Red, Red); // State machine. }
// The player uses hybrid EC - it's a derived entity with components inside. // You also can ditch components entirely and only use entities. // I recommend useng hybrid entities in places, where EC is not entirely needed. // For example, if you know that this entity's code will not be reused anywhere else. public Player(Layer layer, Vector2 position) : base(layer) { _playerSprite = ResourceHub.GetResource <Sprite>("DefaultSprites", "Player"); // You can add components right in the constructor. AddComponent(new PositionComponent(position)); AddComponent(new ActorComponent(_playerSprite)); }
public SpriteDemo(Layer layer) : base(layer) { _monofoxeSprite = ResourceHub.GetResource <Sprite>("DefaultSprites", "Test"); _fireSprite = ResourceHub.GetResource <Sprite>("DefaultSprites", "Fire"); _batch = new SpriteBatch(GraphicsMgr.Device); _seizure = ResourceHub.GetResource <Effect>("Effects", "Seizure"); InitSurface(); }
public override void Draw() { base.Draw(); var canvasSize = GameMgr.WindowManager.CanvasSize; Text.CurrentFont = ResourceHub.GetResource <IFont>("Fonts", "Arial"); Text.HorAlign = TextAlign.Left; Text.VerAlign = TextAlign.Top; // Description. if (CurrentFactory.Description != "") { var padding = 8; var textSize = Text.CurrentFont.MeasureString(CurrentFactory.Description); var origin = Vector2.UnitX * (canvasSize - (textSize + Vector2.One * padding * 2)); GraphicsMgr.CurrentColor = _barColor; RectangleShape.Draw(origin, origin + textSize + Vector2.One * padding * 2, false); GraphicsMgr.CurrentColor = _textColor; Text.Draw(CurrentFactory.Description, Vector2.One * padding + origin); } // Description. // Bottom bar. GraphicsMgr.VertexBatch.PushViewMatrix(); GraphicsMgr.VertexBatch.View = Matrix.CreateTranslation(new Vector3(0, canvasSize.Y - _barHeight, 0)) * GraphicsMgr.VertexBatch.View; GraphicsMgr.CurrentColor = _barColor; RectangleShape.Draw(Vector2.Zero, canvasSize, false); GraphicsMgr.CurrentColor = _textColor; Text.Draw( "fps: " + GameMgr.Fps + " | Current scene: " + CurrentScene.Name + Environment.NewLine + _prevSceneButton + "/" + _nextSceneButton + " - change scene, " + _restartButton + " - restart current scene, " + _toggleUIButton + " - toggle UI, " + _toggleFullscreenButton + " - toggle fullscreen" + Environment.NewLine + CameraController.UpButton + "/" + CameraController.DownButton + "/" + CameraController.LeftButton + "/" + CameraController.RightButton + " - move camera, " + CameraController.ZoomInButton + "/" + CameraController.ZoomOutButton + " - zoom, " + CameraController.RotateLeftButton + "/" + CameraController.RotateRightButton + " - rotate" , _indent ); GraphicsMgr.VertexBatch.PopViewMatrix(); // Bottom bar. }
public Bot(Layer layer) : base(layer) { var botSprite = ResourceHub.GetResource <Sprite>("DefaultSprites", "Bot"); AddComponent(new PositionComponent(Vector2.Zero)); _actor = AddComponent(new ActorComponent(botSprite)); // It is recommended to reuse random objects. TurningSpeed = ECDemoFactory.Random.Next(120, 240); }
public TiledDemo(Layer layer) : base(layer) { // TiledMap which is loaded from Content, is just a data structure // describing the map. We need to make an actual Scene object with entities on it. // You can write your own map builder, or use the default one. // Default map builder can also be expanded. _testMap = ResourceHub.GetResource <TiledMap>("Maps", "Test"); _builder = new SolidMapBuilder(_testMap); _builder.Build(); }
public override void Load() { if (Loaded) { return; } Loaded = true; AddResource("Arial", new Font(_content.Load <SpriteFont>("Arial"))); var fontSprite = ResourceHub.GetResource <Sprite>("DefaultSprites", "Font"); AddResource("FancyFont", new TextureFont(fontSprite, 1, 1, Ascii, false)); }
public GameController() : base(SceneMgr.GetScene("default")["default"]) { GameMgr.MaxGameSpeed = 60; GameMgr.MinGameSpeed = 60; // Fixing framerate on 60. Camera.BackgroundColor = new Color(30, 24, 24); GameMgr.WindowManager.CanvasSize = Camera.Size; GameMgr.WindowManager.Window.AllowUserResizing = false; GameMgr.WindowManager.ApplyChanges(); GameMgr.WindowManager.CenterWindow(); GameMgr.WindowManager.CanvasMode = CanvasMode.Fill; GraphicsMgr.VertexBatch.SamplerState = SamplerState.PointClamp; _monofoxe = ResourceHub.GetResource <Sprite>("DefaultSprites", "Monofoxe"); Text.CurrentFont = ResourceHub.GetResource <IFont>("Fonts", "Arial"); }
public VertexBatchTest(Layer layer) : base(layer) { _monofoxeSprite = ResourceHub.GetResource <Sprite>("DefaultSprites", "AutismCat"); _vbatch = new VertexBatch( GraphicsMgr.Device, null, SamplerState.PointWrap, null, null ); _triangleListVertices = new VertexPositionColorTexture[5]; _triangleListVertices[0] = new VertexPositionColorTexture(new Vector3(100, 100, 0), Color.White, new Vector2(0, 0)); _triangleListVertices[1] = new VertexPositionColorTexture(new Vector3(100 + 32, 100, 0), Color.Red, new Vector2(1, 0)); _triangleListVertices[2] = new VertexPositionColorTexture(new Vector3(100 + 32, 100 + 32, 0), Color.White, new Vector2(1, 1)); _triangleListVertices[3] = new VertexPositionColorTexture(new Vector3(100 + 32, 100 + 64, 0), Color.White, Vector2.Zero); _triangleListVertices[4] = new VertexPositionColorTexture(new Vector3(100 - 32, 100 + 64, 0), Color.White, Vector2.Zero); _triangleListIndices = new short[] { 0, 1, 2, 0, 2, 3, 0, 3, 4, }; _lineListVertices = new VertexPositionColorTexture[5]; _lineListVertices[0] = new VertexPositionColorTexture(new Vector3(10, 10, 0), Color.White, new Vector2(0, 0)); _lineListVertices[1] = new VertexPositionColorTexture(new Vector3(10 + 32, 10, 0), Color.Red, new Vector2(1, 0)); _lineListVertices[2] = new VertexPositionColorTexture(new Vector3(00 + 32, 10 + 32, 0), Color.White, new Vector2(1, 1)); _lineListVertices[3] = new VertexPositionColorTexture(new Vector3(00 + 32, 10 + 64, 0), Color.White, Vector2.Zero); _lineListIndices = new short[] { 0, 1, 1, 2, 2, 3, 3, 0 }; }
public override void Draw() { base.Draw(); var startingPosition = new Vector2(64, 64); var position = startingPosition; var spacing = 100; GraphicsMgr.CurrentColor = _mainColor; // This position accounts for current camera transform matrix. // Visually it will be at the pointer's position when camera will move. CircleShape.Draw(Input.MousePosition, 4, false); // This position only accounts for screen transformation. // When the camera will move, it will offset. CircleShape.Draw(Input.ScreenMousePosition, 8, true); // You can also get mouse position from any camera. // This method can be used in Update, when no camera is active. CircleShape.Draw(GraphicsMgr.CurrentCamera.GetRelativeMousePosition(), 12, true); Text.CurrentFont = ResourceHub.GetResource <IFont>("Fonts", "Arial"); Text.Draw("Keyboard input: " + _keyboardInput.ToString(), position); // Gamepad, mouse and keyboard buttons are using the same method. position += Vector2.UnitY * 64; CircleShape.Draw(position, 16, Input.CheckButton(KeyboardTestButton)); position += Vector2.UnitX * 64; CircleShape.Draw(position, 16, Input.CheckButton(GamepadTestButton)); position += Vector2.UnitX * 64; CircleShape.Draw(position, 16, Input.CheckButton(MouseTestButton)); position = new Vector2(200, 200); if (Input.GamepadConnected(0)) { Text.Draw("Gamepad is connected!", position); } else { Text.Draw("Gamepad is not connected.", position); } // Sticks. position += Vector2.UnitY * 96; CircleShape.Draw(position, 64, true); CircleShape.Draw(position + Input.GamepadGetLeftStick(0) * 64 * new Vector2(1, -1), 16, false); position += Vector2.UnitX * (128 + 64); CircleShape.Draw(position, 64, true); CircleShape.Draw(position + Input.GamepadGetRightStick(0) * 64 * new Vector2(1, -1), 16, false); // Triggers. position -= Vector2.UnitX * (64 + 16); RectangleShape.DrawBySize(position + Vector2.UnitY * Input.GamepadGetRightTrigger(0) * 64, Vector2.One * 8, false); LineShape.Draw(position, position + Vector2.UnitY * 64); position -= Vector2.UnitX * 32; RectangleShape.DrawBySize(position + Vector2.UnitY * Input.GamepadGetLeftTrigger(0) * 64, Vector2.One * 8, false); LineShape.Draw(position, position + Vector2.UnitY * 64); }
public override void Draw() { base.Draw(); var startingPosition = new Vector2(100, 100); var position = startingPosition; var spacing = 100; // Sprites can't have static methods. _monofoxeSprite.Draw(position); position += Vector2.UnitX * spacing * 2; // Setting a shader for the sprite. GraphicsMgr.VertexBatch.Effect = _seizure; // If you want to animate the sprite, you must pass a value from 0 to 1 to it. _fireSprite.Draw(position, _animation); GraphicsMgr.VertexBatch.Effect = null; position += Vector2.UnitX * spacing; // You can also access sprite's frame array, if you want to draw a specific frame. _fireSprite[2].Draw(position, _fireSprite.Origin); position += Vector2.UnitX * spacing; // You can scale, rotate srites and set custom origin point. _fireSprite.Draw( position, 0.4f, new Vector2(_fireSprite.Width, _fireSprite.Height) / 2, new Vector2(1, 2) * (float)Math.Sin(_animation * Math.PI * 2 * 2), new Angle(360 * _animation), Color.Red ); position += Vector2.UnitX * spacing; // You also can draw only a part of the sprite. _monofoxeSprite.Draw( new RectangleF(position.X, position.Y, 64, 64), 0, new RectangleF(64, 64, 64, 64), Angle.Right, Color.White ); position += Vector2.UnitY * spacing * 1.5f; position.X = 0; // You can extract raw texture from the frames. Note that you will get the whole texture atlas. var texture = _monofoxeSprite[0].Texture; var texturePosition = _monofoxeSprite[0].TexturePosition; // This will give you texture's position on the atlas. // We can also use default Monogame's SpriteBatch (or anything, for that matter). // But beforehand we must flush Monofoxe's own batcher. // This method draws all batched graphics. GraphicsMgr.VertexBatch.FlushBatch(); // After that, you can draw anything you like using any method. _batch.Begin( SpriteSortMode.Deferred, null, SamplerState.PointWrap, null, null, null, GraphicsMgr.VertexBatch.View ); _batch.Draw(texture, position, GraphicsMgr.CurrentColor); _batch.End(); // After you're done, you can draw anything you like without switching graphics mode again. RectangleShape.Draw(position, position + new Vector2(texture.Width, texture.Height), true); position += Vector2.UnitX * 512; GraphicsMgr.CurrentColor = Color.Red; Surface.SetTarget(_surface); var po = new Vector2(_surface.Width, _surface.Height) / 2 + new Angle(GameMgr.ElapsedTimeTotal * 10).ToVector2() * 64; RectangleShape.DrawBySize(po, Vector2.One * 8, false); Surface.ResetTarget(); _surface.Draw(position); position += new Vector2(16, 150); GraphicsMgr.CurrentColor = Color.White; Text.CurrentFont = ResourceHub.GetResource <IFont>("Fonts", "Arial"); Text.Draw("This text is drawn using default" + Environment.NewLine + "Monogame spritefont.", position); position += Vector2.UnitY * 48; Text.CurrentFont = ResourceHub.GetResource <IFont>("Fonts", "FancyFont"); Text.Draw("This text is drawn using custom" + Environment.NewLine + "font made from a sprite.", position, Vector2.One * 1.1f, Vector2.Zero, new Angle(-10)); }
public PrimitiveDemo(Layer layer) : base(layer) { // Primitives can only be drawn from instances. There are no static methods. _trianglefan = new TriangleFanPrimitive(5); _trianglefan.Vertices[0] = new Vertex(new Vector2(0, 0)); _trianglefan.Vertices[1] = new Vertex(new Vector2(16, 0)); _trianglefan.Vertices[2] = new Vertex(new Vector2(40, 10)); _trianglefan.Vertices[3] = new Vertex(new Vector2(64, 64)); _trianglefan.Vertices[4] = new Vertex(new Vector2(0, 32)); _trianglestrip = new TriangleStripPrimitive(8); _trianglestrip.Vertices[0] = new Vertex(new Vector2(0, 0), _mainColor); _trianglestrip.Vertices[1] = new Vertex(new Vector2(32, 32), _secondaryColor); _trianglestrip.Vertices[2] = new Vertex(new Vector2(64, 0), _secondaryColor2); _trianglestrip.Vertices[3] = new Vertex(new Vector2(96, 32), _secondaryColor); _trianglestrip.Vertices[4] = new Vertex(new Vector2(64 + 32, 0), _secondaryColor2); _trianglestrip.Vertices[5] = new Vertex(new Vector2(96 + 32, 32), _secondaryColor); _trianglestrip.Vertices[6] = new Vertex(new Vector2(64 + 64, 0), _secondaryColor2); _trianglestrip.Vertices[7] = new Vertex(new Vector2(96 + 64, 32), _mainColor); _mesh = new MeshPrimitive(16, 16); _mesh.Position = new Vector2(0, 100); // You can set the texture for a primitive. Preferrably it shouldn't be in texture atlas. // If in atlas, textures wouldn't be able to repeat. _autismCatSprite = ResourceHub.GetResource <Sprite>("DefaultSprites", "AutismCat"); _mesh.SetTextureFromFrame(_autismCatSprite[0]); var vIndex = 0; for (var k = 0; k < _mesh.Height; k += 1) { for (var i = 0; i < _mesh.Width; i += 1) { _mesh.Vertices[vIndex] = new Vertex( Vector2.Zero, // Positions will be set later. Color.White, new Vector2(i / (float)(_mesh.Width - 1), k / (float)(_mesh.Height - 1)) * _meshRepeat ); vIndex += 1; } } _linestrip = new LineStripPrimitive(16); for (var i = 0; i < 16; i += 1) { _linestrip.Vertices[i] = new Vertex(Vector2.Zero, new Color(16 * i, 8 * i, 4 * i)); } // You can make your own custom primitives. // CustomTrianglePrimitive and CustomLinePrimitive give you // access to the index array. Index array tells inwhat order vertices should be drawn. // One vertex can be used multiple times in an index array. _custom = new CustomTrianglePrimitive(4); _custom.Vertices[0] = new Vertex(new Vector2(0, 0), Color.Green); _custom.Vertices[1] = new Vertex(new Vector2(32, 0)); _custom.Vertices[2] = new Vertex(new Vector2(32, 32)); _custom.Vertices[3] = new Vertex(new Vector2(64, 32), Color.Blue); _custom.Indices = new short[] { 0, 1, 2, 1, 3, 2 }; }