I moved to a new URL! Check it out!

posts tagged with: programming

Otter Example: Tilemaps

Otter Example: Tilemaps
The next Otter example is up and deals with the wild world of using tilemaps. The following example is a way to drop some tiles into a scene and also use the mouse to edit them.

namespace TilemapExample {
class Program {
static void Main(string[] args) {
// Create a Game.
var game = new Game("Tilemap Example");

// Create a Scene.
var scene = new Scene();
// Add the Tiles Entity to the Scene.
scene.Add(new Tiles());

// Set the mouse visibility to true for this example.
game.MouseVisible = true;

// Start the Game.
game.Start(scene);
}
}

class Tiles : Entity {
// The Tilemap Graphic to use for rendering tiles.
public Tilemap Tilemap;
// The grid size to use for the Tilemap.
public static int GridSize = 32;
// The current selected tile to place.
public int CurrentTile;

public Tiles() : base() {
// Create the Tilemap the size of the game window using the defined grid size.
Tilemap = new Tilemap("tiles.png", Game.Instance.Width, Game.Instance.Height, GridSize, GridSize);
// Add the Tilemap to the list of Graphics to render.
AddGraphic(Tilemap);

// Place some tiles.
Tilemap.SetTile(0, 0, 0);
Tilemap.SetTile(1, 0, 0);
Tilemap.SetTile(2, 0, 0);

// Place some more tiles.
Tilemap.SetTile(0, 4, 1);
Tilemap.SetTile(1, 4, 1);
Tilemap.SetTile(2, 4, 1);

// Even more tiles.
Tilemap.SetTile(0, 8, 2);
Tilemap.SetTile(1, 8, 2);
Tilemap.SetTile(2, 8, 2);

// Want more tiles?
Tilemap.SetTile(0, 12, 3);
Tilemap.SetTile(1, 12, 3);
Tilemap.SetTile(2, 12, 3);
}

public override void Update() {
base.Update();

// Determine the grid position of the mouse.
var gridX = (int)Util.SnapToGrid(Input.MouseX, GridSize) / GridSize;
var gridY = (int)Util.SnapToGrid(Input.MouseY, GridSize) / GridSize;

// Switch the current tile to place based on the mouse wheel movement.
CurrentTile += Input.MouseWheelDelta;
CurrentTile %= 4; // Make sure the current tile is always 0 to 3.

if (Input.MouseWheelDelta != 0) {
// Log the current tile value.
Console.WriteLine("Current Tile is now {0}", CurrentTile);
}

// Place a tile when the left mouse button is down.
if (Input.MouseButtonDown(MouseButton.Left)) {
Tilemap.SetTile(gridX, gridY, CurrentTile);
}
// Remove a tile when the right mouse button is down.
if (Input.MouseButtonDown(MouseButton.Right)) {
Tilemap.ClearTile(gridX, gridY);
}
}
}
}
Make sure to download the tilemap source image as well. The final result should end up something like this:

Image


Even more examples on the way!

Otter Example: More Collisions

Otter Example: More Collisions
Hot of the presses with a new Otter example! This example goes over more of the collider types that can be used in Otter. In total Otter has a Box, Circle, Grid, Polygon, Pixel, Point, and Line collider, and this example will cover all of them except for the Pixel collider (because that one should be used very sparingly anyway.)

Here's a quick preview of the code that can be found in the example:
class PolygonTest : Entity {

PolygonCollider polygonCollider;

public PolygonTest(float x, float y) : base(x, y) {
// Create a PolygonCollider and give it some Vector2 points.
polygonCollider = new PolygonCollider(
new Vector2(0, 0),
new Vector2(80, -10),
new Vector2(30, 90),
new Vector2(-5, 15),
new Vector2(0, 5)
);

// Add the Walls tag (since this isn't in the constructor.)
polygonCollider.AddTag(Tags.Walls);

// Set the rotation to a random angle.
polygonCollider.Rotation = Rand.Angle;

// Add the Collider.
AddCollider(polygonCollider);
// Center the origin of the Collider.
polygonCollider.CenterOrigin();
}

public override void Render() {
base.Render();

// Render the Collider for debug purposes.
polygonCollider.Render();
}
}
The full example places a bunch of colliders in a scene with a moving object to test collisions with.

Image


A lot more examples are on the way!

Otter Example: Player Sessions

Otter Example: Player Sessions
Things have been pretty hectic for me for the past week so unfortunately all I've really had time to do is more Otter examples.

Image


A Session in Otter is a cool way to keep track of a player playing your game. You can set the controls in a session, and also keep track of other data such as high scores, levels unlocked, whatever else. Sessions also have an easy way to export data in a semi-encrypted format to prevent most players from tampering with it.

The full example is just a click away and you should totally check it out!

Otter Example: Basic Collision

Otter Example: Basic Collision
Fresh out of the oven is another example for Otter, the 2d game making framework based on SFML.net. The new example covers basic collision detection and response.

Image


On the examples page I've listed out all of the examples I want to cover in the next few weeks. I'm going to try to keep working on posting an example every few days or so, but I have some upcoming travel that might make that difficult. We'll see!

Oh and so many people are sending me this image so I might as well post it here:

Image

Otter Example: Spritemap Animations

Otter Example: Spritemap Animations
I want to keep the Otter example train rolling until I manage to flesh out enough examples to cover all the basics of making a game. The newest example covers animating sprites using a quick example sprite sheet from the Spriter's Resource.

The example with all of the code and required image is available right here.

Image


Also a reminder: if you're using Otter actively make sure to stop by the forums, or sign up for the relatively new Otter Slack Team. There's actually quite a bit of activity happening in the Slack, and recently one of the Otter community members posted a new GUI suite of components to use in your Otter made games!

Otter Example: Loading Images

Otter Example: Loading Images
A new example for Otter is now available! The new example covers loading images into a game. This example assumes that you have a decent understanding of C# already as it uses some stuff like local variables and extending classes and all that cool stuff.

The example also covers using Visual Studio to manage external resources. The most simple way of using a small amount of resources is to have Visual Studio copy the files into the bin directory. So check out the example for all those details!