Dev Log: Code Sample
I'm slowly getting back into the groove of programming again after a long hiatus. In case you're just tuning in, I'm working on a C# SFML framework for 2d games. Right now I'm calling the framework Otter, and eventually when I get it to a more presentable state I'll be open sourcing it on BitBucket somewhere so other people can use it, and contribute to it.
For now here's a small teaser of how some code looks when using the framework. This is for the entity that the player controls with mouse and keyboard or a PC game controller. It flies around in a top down view in the scene. It's a big chunk of code, so I'm putting it behind the "read more" link!
I know it's a little difficult to just look at a bunch of code and decipher what's happening, but hopefully this gives you a rough idea of some of the features the framework has going on.
For now here's a small teaser of how some code looks when using the framework. This is for the entity that the player controls with mouse and keyboard or a PC game controller. It flies around in a top down view in the scene. It's a big chunk of code, so I'm putting it behind the "read more" link!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Otter;
namespace Gaiaden.Mission {
class Guardian : Entity {
Image image = new Image("guardian.png");
Image imageGlow = new Image("guardianglow.png");
BoxCollider hitBox = new BoxCollider(40, 60, (int)Tags.Player);
BasicMovement movement = new BasicMovement(1500, 1500, 30);
Axis axis = new Axis(Key.Up, Key.Right, Key.Down, Key.Left);
Button trigger = new Button();
Button menu = new Button();
Button pause = new Button();
Heart heart = new Heart(100, 2);
Image islandGridImage;
uint shootTimer = 0;
public static Guardian Instance;
float oldX, oldY;
public Guardian(float x, float y) : base(x, y) {
AddGraphic(imageGlow);
AddGraphic(image);
image.CenterOrigin();
imageGlow.CenterOrigin();
AddComponent(movement);
AddComponent(axis);
AddComponent(trigger);
AddComponent(menu);
AddCollider(hitBox);
Instance = this;
islandGridImage = Image.CreateRectangle(Gaiaden.ISLAND_GRID_SIZE, new Color("00000020"));
trigger.AddMouseButton(MouseButton.Left)
.AddButton(0, 0)
.AddButton(1, 0)
.AddButton(0, 1)
.AddButton(6)
.AddAxisButton(AxisButton.ZMinus);
pause.AddKey(Key.Space);
menu.AddMouseButton(MouseButton.Right)
.AddKey(Key.Space);
axis.AddAxis(JoyAxis.X, JoyAxis.Y)
.AddKeys(Key.W, Key.D, Key.S, Key.A);
movement.OnMove += postMovement;
movement.Axis = axis;
Group = (int)Groups.Action;
}
public override void Update() {
base.Update();
if (trigger.Down) {
if (shootTimer % 5 == 0) {
Scene.Add(new Bullet(X, Y, Cursor.Instance.ScreenX, Cursor.Instance.ScreenY));
}
shootTimer++;
}
else {
shootTimer = 0;
}
if (menu.Pressed) {
Gaiaden.BuildMenu();
}
if (DebugInput.Instance.KeyPressed(Key.T)) {
Scene.Add(new ActionText(X, Y - 60, "+40"));
}
imageGlow.ScaleX = Util.RandomRange(0.9f, 1.1f);
imageGlow.ScaleY = Util.RandomRange(0.9f, 1.1f);
imageGlow.Angle = Util.RandomRange(-5f, 5f);
islandGridImage.X = Util.SnapFloor(X, Gaiaden.ISLAND_GRID_SIZE);
islandGridImage.Y = Util.SnapFloor(Y, Gaiaden.ISLAND_GRID_SIZE);
}
void postMovement() {
if (X < Gaiaden.SCENE_PADDING) {
X = Gaiaden.SCENE_PADDING;
movement.Speed.X = 0;
}
if (X > Scene.Width - Gaiaden.SCENE_PADDING) {
X = Scene.Width - Gaiaden.SCENE_PADDING;
movement.Speed.X = 0;
}
if (Y < Gaiaden.SCENE_PADDING) {
Y = Gaiaden.SCENE_PADDING;
movement.Speed.Y = 0;
}
if (Y > Scene.Height - Gaiaden.SCENE_PADDING) {
Y = Scene.Height - Gaiaden.SCENE_PADDING;
movement.Speed.Y = 0;
}
Scene.Add(new GuardianTrail(X, Y));
if (oldX != X && oldY != Y) {
Scene.Add(new GuardianTrail((oldX + X) / 2, (oldY + Y) / 2));
}
oldX = X;
oldY = Y;
}
public override void Render() {
Draw.Graphic(islandGridImage, 0, 0);
base.Render();
}
}
}
I know it's a little difficult to just look at a bunch of code and decipher what's happening, but hopefully this gives you a rough idea of some of the features the framework has going on.
Comments
Post your comment!