Dev Log: Code Snippets
Yesterday I posted about my amazing tile collisions, so I thought I'd post a quick code example on some of the things involved with setting it up. This isn't any of the code from the actual collision detection, but it's code that most users would see if they ever use this framework.
This is the code for the moving white box:
Here's the code for the movement component that the white box uses:
And here's the scene that sets up the little demo of moving the box around a tile map:
Pretty straight forward (I think!) You can see a lot of similarities to Flashpunk in here. I will be releasing the full source of this framework pretty soon, I just need to fix a few more annoying things before it goes into the hands of other people, and then hopefully people smarter than me can help improve it, haha!
This is the code for the moving white box:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Otter;
namespace HelloSFML {
class BoxDude : Entity {
BasicMovement movement = new BasicMovement(500, 500, 50);
Image img;
BoxCollider hitbox;
Axis axis = new Axis();
uint size = 50;
public BoxDude() {
refreshSize();
AddComponent(movement);
AddComponent(axis);
movement.CollidesWith.Add(1);
axis.AddKeys(Key.W, Key.D, Key.S, Key.A);
movement.Axis = axis;
}
void refreshSize() {
RemoveCollider(hitbox);
RemoveGraphic(img);
img = Image.CreateRectangle(size);
hitbox = new BoxCollider(size, size, 0);
AddCollider(hitbox);
SetGraphic(img);
movement.Collider = hitbox;
}
public override void Update() {
base.Update();
img.Color = Color.White;
if (Collide(X, Y, 1) != null) {
img.Color = Color.Red;
}
if (Input.Instance.KeyPressed(Key.Period)) {
size -= 10;
refreshSize();
}
if (Input.Instance.KeyPressed(Key.Comma)) {
size += 10;
refreshSize();
}
}
}
}
Here's the code for the movement component that the white box uses:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Otter {
public class BasicMovement : Movement {
public Speed Speed;
public Speed TargetSpeed;
public uint Accel;
public uint SpeedScale = 100;
public Axis Axis;
public Collider Collider;
public List<int> CollidesWith = new List<int>();
int moveBufferX = 0;
int moveBufferY = 0;
public BasicMovement(float xMax, float yMax, uint accel) {
Speed = new Speed(xMax, yMax);
TargetSpeed = new Speed(xMax, yMax);
Accel = accel;
}
public override void Update() {
base.Update();
TargetSpeed.X = Axis.X * TargetSpeed.XMax;
TargetSpeed.Y = Axis.Y * TargetSpeed.YMax;
Speed.X = Util.Approach(Speed.X, TargetSpeed.X, Accel);
Speed.Y = Util.Approach(Speed.Y, TargetSpeed.Y, Accel);
moveBufferX += (int)Speed.X;
moveBufferY += (int)Speed.Y;
while (Math.Abs(moveBufferX) >= SpeedScale) {
int move = Math.Sign(moveBufferX);
if (Collider != null) {
foreach (int i in CollidesWith) {
if (Collider.Collide(Entity.X + move, Entity.Y, i) == null) {
Entity.X += move;
moveBufferX = (int)Util.Approach(moveBufferX, 0, SpeedScale);
}
else {
moveBufferX = 0;
Speed.X = 0;
}
}
}
else {
Entity.X += move;
moveBufferX = (int)Util.Approach(moveBufferX, 0, SpeedScale);
}
}
while (Math.Abs(moveBufferY) >= SpeedScale) {
int move = Math.Sign(moveBufferY);
if (Collider != null) {
foreach (int i in CollidesWith) {
if (Collider.Collide(Entity.X, Entity.Y + move, i) == null) {
Entity.Y += move;
moveBufferY = (int)Util.Approach(moveBufferY, 0, SpeedScale);
}
else {
moveBufferY = 0;
Speed.Y = 0;
}
}
}
else {
Entity.Y += move;
moveBufferY = (int)Util.Approach(moveBufferY, 0, SpeedScale);
}
}
if (OnMove != null) {
OnMove();
}
}
}
}
And here's the scene that sets up the little demo of moving the box around a tile map:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Otter;
namespace HelloSFML {
class SceneGridTest : Scene {
Tilemap tiles = new Tilemap("assets/tiles.png", 2000, 800, 64, 64);
GridCollider grid = new GridCollider(2000, 2000, 64, 64);
Entity e = new Entity();
public SceneGridTest() {
for (int i = 0; i < 100; i++) {
uint tx, ty;
tx = (uint)Util.RandomRangeInt((int)tiles.TileColumns);
ty = (uint)Util.RandomRangeInt((int)tiles.TileRows);
tiles.SetTile(tx, ty, (uint)Util.RandomRangeInt(3));
grid.SetTile(tx, ty, true);
}
grid.AddTag(1);
Add(new BoxDude());
e.AddGraphic(tiles);
e.AddCollider(grid);
e.Layer = 100;
Add(e);
}
}
}
Pretty straight forward (I think!) You can see a lot of similarities to Flashpunk in here. I will be releasing the full source of this framework pretty soon, I just need to fix a few more annoying things before it goes into the hands of other people, and then hopefully people smarter than me can help improve it, haha!
Post your comment!