Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Forked from jrmoserbaltimore/RoomObject.cs
Last active February 22, 2016 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hagbarddenstore/b71dee2fdec6e1e20357 to your computer and use it in GitHub Desktop.
Save hagbarddenstore/b71dee2fdec6e1e20357 to your computer and use it in GitHub Desktop.
Horrendous code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using BlackCore.GameWorld;
namespace BlackCore.GameWorld
{
public interface IRoom
{
IRoom SelectMode(Dictionary<string, int> mode, int state);
int Tile(int x, int y, int layer);
int RealTile(int x, int y, int layer);
void SetTile(int x, int y, int layer, int tile);
int Tile(int x, int y, int layer, Dictionary<string, int> mode, int state);
int RealTile(int x, int y, int layer, Dictionary<string, int> mode, int state);
void SetTile(int x, int y, int layer, int tile, Dictionary<string, int> mode, int state);
}
public class RoomProxy : IRoom
{
protected Dictionary<string, int> mode;
protected int state;
protected IRoom room;
RoomProxy(Dictionary<string, int> mode, int state, IRoom room)
{
if (mode == null)
{
throw new System.ArgumentNullException("mode","Cannot be null") ;
}
if (room == null)
{
throw new System.ArgumentNullException("room","Cannot be null") ;
}
this.mode = mode;
this.state = state;
this.room = room;
}
IRoom SelectMode(Dictionary<string, int> mode, int state)
{
room.SelectMode(mode, state);
}
int Tile(int x, int y, int layer)
{
room.Tile(x, y, layer, mode, state);
}
int RealTile(int x, int y, int layer)
{
room.RealTile(x, y, layer, mode, state);
}
void SetTile(int x, int y, int layer, int tile)
{
room.SetTile(x, y, layer, tile, mode, state);
}
int Tile(int x, int y, int layer, Dictionary<string, int> mode, int state)
{
room.Tile(x, y, layer, mode, state);
}
int RealTile(int x, int y, int layer, Dictionary<string, int> mode, int state)
{
room.RealTile(x, y, layer, mode, state);
}
void SetTile(int x, int y, int layer, int tile, Dictionary<string, int> mode, int state)
{
room.SetTile(x, y, layer, tile, mode, state);
}
}
public class Room : IRoom
{
protected string childMode = null;
protected Dictionary<int, IRoom> children = null;
protected Dictionary<int, IRoom> states = null;
protected int width;
protected int height;
Room (int width, int height)
{
this.width = width;
this.height = height;
}
Room (int width, int height, string childMode, Dictionary<int, IRoom> children)
: this(width, height)
{
this.childMode = childMode;
this.children = children;
}
IRoom SelectMode(Dictionary<string, int> mode, int state)
{
return RoomProxy(mode, state, this);
}
int Tile(int x, int y, int layer)
{
return Tile(x, y, layer, null, 0);
}
int RealTile(int x, int y, int layer)
{
return RealTile(x, y, layer, null, 0);
}
void SetTile(int x, int y, int layer, int tile)
{
SetTile(x, y, layer, tile, null, 0);
}
int Tile(int x, int y, int layer, Dictionary<string, int> mode, int state)
{
var child = GetChild(mode);
if (child != null)
{
return child.Tile(x, y, layer, mode, state);
}
else if (states.ContainsKey(state))
{
return states[state].Tile(x, y, layer);
}
else if (states.ContainsKey(0))
{
return this.states[0].Tile(x, y, layer);
}
throw new System.InvalidOperationException("No default state 0");
}
int RealTile(int x, int y, int layer, Dictionary<string, int> mode, int state)
{
var child = GetChild(mode);
if (child != null)
{
return child.RealTile(x, y, layer, mode, state);
}
else if (states.ContainsKey(state))
{
return states[state].RealTile(x, y, layer);
}
else if (states.ContainsKey(0))
{
return this.states[0].RealTile(x, y, layer);
}
throw new System.InvalidOperationException("No default state 0");
}
void SetTile(int x, int y, int layer, int tile, Dictionary<string, int> mode, int state)
{
var child = GetChild(mode);
if (child != null)
{
child.SetTile(x, y, layer, mode, state);
}
else
{
if (!states.ContainsKey(state))
{
states.Add(state, new RoomState(width, height));
}
states[state].SetTile(x, y, layer, tile);
}
}
protected IRoom GetChild(Dictionary<string, int> mode)
{
IRoom child;
if (mode.ContainsKey(childMode))
{
if (!children.TryGetValue(mode[childMode], child))
{
// FIXME: Throw exception
}
return child;
}
return null;
}
}
public class RoomState : IRoom
{
protected int[,,] tiles = null;
RoomState (int width, int height)
{
tiles = new int[width, height, 4];
}
IRoom SelectMode(Dictionary<string, int> mode, int state)
{
return this;
}
int Tile(int x, int y, int layer)
{
return tiles[x,y,layer];
}
int RealTile(int x, int y, int layer)
{
return Tile(x, y, layer);
}
void SetTile(int x, int y, int layer, int tile)
{
tiles[x,y,layer] = tile;
}
int Tile(int x, int y, int layer, Dictionary<string, int> mode, int state)
{
return Tile(x, y, layer);
}
int RealTile(int x, int y, int layer, Dictionary<string, int> mode, int state)
{
return RealTile(x, y, layer);
}
void SetTile(int x, int y, int layer, int tile, Dictionary<string, int> mode, int state)
{
SetTile(x, y, layer, tile);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment