public CustomPropertyDescriptor(CustomProperty prop) : base(prop.Name, null) { this.prop = prop; }
// Take in a GameEntity, create new GameEntity, set new entity with new values equal to original GameEntity public static GameEntity CloneEntity(GameEntity ge) { GameEntity newGE = new GameEntity(); int clonePosOffset = 6; newGE.Type = ge.Type; switch (newGE.Type) { case EntityType.CIRCLE: newGE.Props.TryAdd(new CustomProperty { Name = "OutlineColor", Type = typeof(Color), DefaultValue = (Color)ge.Props["OutlineColor"] }); newGE.Props.TryAdd(new CustomProperty { Name = "FillColor", Type = typeof(Color), DefaultValue = (Color)ge.Props["FillColor"] }); CustomProperty rad = new CustomProperty { Name = "Radius", Type = typeof(int), DefaultValue = (int)ge.Props["Radius"] }; newGE.Props.TryAdd(rad); Point gePos = (Point)ge.Props["Position"]; gePos.X += clonePosOffset; gePos.Y += clonePosOffset; CustomProperty pos = new CustomProperty { Name = "Position", Type = typeof(Point), DefaultValue = gePos }; newGE.Props.TryAdd(pos); newGE.SetBoundingBox = new delSetBoundingBox(delegate(Rectangle r) { newGE.Props["Position"] = r.Location; /* Only height or width can be used, averaging them makes resizing difficult */ newGE.Props["Radius"] = r.Size.Width; }); newGE.GetBoundingBox = new delGetBoundingBox(delegate() { /* Generate a bounding box based on the radius. */ Rectangle bound; if (newGE.Props["Position"] == null || newGE.Props["Radius"] == null) { bound = new Rectangle((Point)pos.DefaultValue, new Size((int)rad.DefaultValue, (int)rad.DefaultValue)); } else { bound = new Rectangle((Point)newGE.Props["Position"], new Size((int)newGE.Props["Radius"], (int)newGE.Props["Radius"])); } return(bound); }); break; case EntityType.RECT: newGE.Props.TryAdd(new CustomProperty { Name = "OutlineColor", Type = typeof(Color), DefaultValue = (Color)ge.Props["OutlineColor"] }); newGE.Props.TryAdd(new CustomProperty { Name = "FillColor", Type = typeof(Color), DefaultValue = (Color)ge.Props["FillColor"] }); Rectangle geRect = (Rectangle)ge.Props["Dimensions"]; geRect.X += clonePosOffset; geRect.Y += clonePosOffset; CustomProperty dim = new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = geRect }; newGE.Props.TryAdd(dim); newGE.SetBoundingBox = new delSetBoundingBox(delegate(Rectangle r) { newGE.Props["Dimensions"] = r; }); newGE.GetBoundingBox = new delGetBoundingBox(delegate() { return(newGE.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)newGE.Props["Dimensions"]); }); break; case EntityType.WAVE_TRACK: newGE.Props.TryAdd(new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = (Rectangle)ge.Props["Dimensions"] }); newGE.Props.TryAdd(new CustomProperty { Name = "Oscillation", Type = typeof(float), DefaultValue = (float)ge.Props["Oscillation"] }); newGE.Props.TryAdd(new CustomProperty { Name = "Speed", Type = typeof(float), DefaultValue = (float)ge.Props["Speed"] }); newGE.Props.TryAdd(new CustomProperty { Name = "Image", Type = typeof(Image), DefaultValue = (Image)ge.Props["Image"] }); newGE.SetBoundingBox = new delSetBoundingBox(delegate(Rectangle r) { newGE.Props["Dimensions"] = r; }); //newGE.GetBoundingBox = new delGetBoundingBox(delegate() //{ // return newGE.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)ge.Props["Dimensions"]; //}); break; case EntityType.CONVERYOR_TRACK: return(null); break; case EntityType.FUNCTION_TRACK: return(null); break; default: return(null); } return(newGE); }
//Create Track Two public static GameEntity CreateTrackTwo(int x, int y, int w, int h, float speed, string filePath, Point scl) { GameEntity ge = new GameEntity(); ge.Type = EntityType.CONVERYOR_TRACK; // Set GameEntities and set property items with the entities properties CustomProperty dim = new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = new Rectangle(x, y, w, h) }; ge.Props.TryAdd(dim); CustomProperty spd = new CustomProperty { Name = "Speed", Type = typeof(float), DefaultValue = speed }; ge.Props.TryAdd(spd); CustomProperty img = new CustomProperty { Name = "Image", Type = typeof(string), DefaultValue = filePath }; ge.Props.TryAdd(img); CustomProperty scale = new CustomProperty { Name = "Scale", Type = typeof(Point), DefaultValue = scl }; ge.Props.TryAdd(scale); // Set the GameEntity Bounding Box ge.SetBoundingBox = new delSetBoundingBox(delegate (Rectangle r) { ge.Props["Dimensions"] = r; }); // Get the GameEntity Bounding Box ge.GetBoundingBox = new delGetBoundingBox(delegate () { return ge.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)ge.Props["Dimensions"]; }); return ge; }
//Try and add a new property. Returns false (and does nothing) if the key is already in the dictionary, // otherwise returns true and adds the property. It sets the value in the dictionary to null, indicating // the default value should be used for now. public bool TryAdd(CustomProperty newCp) { if (values.ContainsKey(newCp.Name)) { return false; } Properties.Add(newCp); values[newCp.Name] = null; return true; }
//Factory method for creating a rectangular game entity. This can be chained with other // factory methods to create new entity types - for example a sprite game entity would // create a rectangle entity first, then have additional logic to add a texture, subrect, etc... public static GameEntity CreateRectangle(int x, int y, int w, int h) { GameEntity ge = new GameEntity(); ge.Type = EntityType.RECT; ge.Props.TryAdd(new CustomProperty { Name = "OutlineColor", Type = typeof(Color), DefaultValue = Color.Black }); ge.Props.TryAdd(new CustomProperty { Name = "FillColor", Type = typeof(Color), DefaultValue = Color.Transparent }); CustomProperty dim = new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = new Rectangle(x,y,w,h) }; ge.Props.TryAdd(dim); //ge.Props.TryAdd(new CustomProperty { Name = "DynamicProperties", Type = typeof(CustomPropertyDictionary), DefaultValue = new CustomPropertyDictionary() }); ge.SetBoundingBox = new delSetBoundingBox(delegate(Rectangle r) { ge.Props["Dimensions"] = r; }); ge.GetBoundingBox = new delGetBoundingBox(delegate() { return ge.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)ge.Props["Dimensions"]; }); return ge; }
public static GameEntity CreateSprite(int x, int y, int w, int h, string filepath) { GameEntity ge = new GameEntity(); ge.Type = EntityType.SPRITE; ge.Props.TryAdd(new CustomProperty { Name = "OutlineColor", Type = typeof(Color), DefaultValue = Color.Black }); ge.Props.TryAdd(new CustomProperty { Name = "FillColor", Type = typeof(Color), DefaultValue = Color.Transparent }); CustomProperty dim = new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = new Rectangle(x, y, w, h) }; ge.Props.TryAdd(dim); //add image path ge.Props.TryAdd(new CustomProperty { Name = "Image", Type = typeof(string), DefaultValue = filepath }); ge.Props.TryAdd(new CustomProperty { Name = "ImageFile", Type = typeof(Image), DefaultValue = filepath }); ge.SetBoundingBox = new delSetBoundingBox(delegate (Rectangle r) { ge.Props["Dimensions"] = r; }); ge.GetBoundingBox = new delGetBoundingBox(delegate () { return ge.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)ge.Props["Dimensions"]; }); //draw associated image string path = "../../../../Assets/" + filepath; Image image = Image.FromFile(path); ge.Props["ImageFile"] = image; return ge; }
// Take in a GameEntity, create new GameEntity, set new entity with new values equal to original GameEntity public static GameEntity CloneEntity(GameEntity ge) { GameEntity newGE = new GameEntity(); int clonePosOffset = 6; newGE.Type = ge.Type; newGE.Props.TryAdd(new CustomProperty { Name = "OutlineColor", Type = typeof(Color), DefaultValue = (Color)ge.Props["OutlineColor"] }); newGE.Props.TryAdd(new CustomProperty { Name = "FillColor", Type = typeof(Color), DefaultValue = (Color)ge.Props["FillColor"] }); switch (newGE.Type) { case EntityType.CIRCLE: CustomProperty rad = new CustomProperty { Name = "Radius", Type = typeof(int), DefaultValue = (int)ge.Props["Radius"] }; newGE.Props.TryAdd(rad); Point gePos = (Point)ge.Props["Position"]; gePos.X += clonePosOffset; gePos.Y += clonePosOffset; CustomProperty pos = new CustomProperty { Name = "Position", Type = typeof(Point), DefaultValue = gePos }; newGE.Props.TryAdd(pos); newGE.SetBoundingBox = new delSetBoundingBox(delegate (Rectangle r) { newGE.Props["Position"] = r.Location; /* Only height or width can be used, averaging them makes resizing difficult */ newGE.Props["Radius"] = r.Size.Width; }); newGE.GetBoundingBox = new delGetBoundingBox(delegate () { /* Generate a bounding box based on the radius. */ Rectangle bound; if (newGE.Props["Position"] == null || newGE.Props["Radius"] == null) { bound = new Rectangle((Point)pos.DefaultValue, new Size((int)rad.DefaultValue, (int)rad.DefaultValue)); } else { bound = new Rectangle((Point)newGE.Props["Position"], new Size((int)newGE.Props["Radius"], (int)newGE.Props["Radius"])); } return bound; }); break; case EntityType.RECT: Rectangle geRect = (Rectangle)ge.Props["Dimensions"]; geRect.X += clonePosOffset; geRect.Y += clonePosOffset; CustomProperty dim = new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = geRect }; newGE.Props.TryAdd(dim); newGE.SetBoundingBox = new delSetBoundingBox(delegate (Rectangle r) { newGE.Props["Dimensions"] = r; }); newGE.GetBoundingBox = new delGetBoundingBox(delegate () { return newGE.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)newGE.Props["Dimensions"]; }); break; default: return null; } return newGE; }
public static GameEntity CreateCircle(int radius, Point position) { GameEntity ge = new GameEntity(); ge.Type = EntityType.CIRCLE; //MIDTERM: Initialise the rest of the entity, including the properties (see CreateRectangle() for reference). // Don't forget you also need to initialise the BoundingBox delegates. ge.Props.TryAdd(new CustomProperty { Name = "OutlineColor", Type = typeof(Color), DefaultValue = Color.Black }); ge.Props.TryAdd(new CustomProperty { Name = "FillColor", Type = typeof(Color), DefaultValue = Color.Transparent }); /* Creating these as named variables so that they can be referred to later. */ CustomProperty rad = new CustomProperty { Name = "Radius", Type = typeof(int), DefaultValue = radius }; ge.Props.TryAdd(rad); CustomProperty pos = new CustomProperty { Name = "Position", Type = typeof(Point), DefaultValue = new Point(position.X, position.Y) }; ge.Props.TryAdd(pos); ge.SetBoundingBox = new delSetBoundingBox(delegate (Rectangle r) { ge.Props["Position"] = r.Location; /* Only height or width can be used, averaging them makes resizing difficult */ ge.Props["Radius"] = r.Size.Width / 2; }); ge.GetBoundingBox = new delGetBoundingBox(delegate () { /* Generate a bounding box based on the radius. */ Rectangle bound; if (ge.Props["Position"] == null || ge.Props["Radius"] == null) { bound = new Rectangle((Point)pos.DefaultValue, new Size((int)rad.DefaultValue * 2, (int)rad.DefaultValue * 2)); } else { bound = new Rectangle((Point)ge.Props["Position"], new Size((int)ge.Props["Radius"] * 2, (int)ge.Props["Radius"] * 2)); } return bound; }); return ge; }
//Create Track Two public static GameEntity CreateTrackTwo(int x, int y, int w, int h) { GameEntity ge = new GameEntity(); ge.Type = EntityType.CONVERYOR_TRACK; CustomProperty dim = new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = new Rectangle(x, y, w, h) }; ge.Props.TryAdd(dim); CustomProperty spd = new CustomProperty { Name = "Speed", Type = typeof(float), DefaultValue = 1.0f }; ge.Props.TryAdd(spd); CustomProperty sprites = new CustomProperty { Name = "Sprites", Type = typeof(List<Image>), DefaultValue = new List<Image>() }; ge.Props.TryAdd(sprites); ge.SetBoundingBox = new delSetBoundingBox(delegate(Rectangle r) { ge.Props["Dimensions"] = r; }); ge.GetBoundingBox = new delGetBoundingBox(delegate() { return ge.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)ge.Props["Dimensions"]; }); return ge; }
//Create Track Three public static GameEntity CreateTrackThree(int x, int y, int w, int h) { GameEntity ge = new GameEntity(); ge.Type = EntityType.FUNCTION_TRACK; CustomProperty dim = new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = new Rectangle(x, y, w, h) }; ge.Props.TryAdd(dim); CustomProperty osc = new CustomProperty { Name = "Oscillation", Type = typeof(float), DefaultValue = 1.0f }; ge.Props.TryAdd(osc); //Place holder prop for function, not sure what this is yet. CustomProperty function = new CustomProperty { Name = "Function", Type = typeof(string), DefaultValue = "PlaceHolder" }; ge.Props.TryAdd(function); CustomProperty sprites = new CustomProperty { Name = "Sprites", Type = typeof(List<Image>), DefaultValue = new List<Image>() }; ge.Props.TryAdd(sprites); ge.SetBoundingBox = new delSetBoundingBox(delegate(Rectangle r) { ge.Props["Dimensions"] = r; }); ge.GetBoundingBox = new delGetBoundingBox(delegate() { return ge.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)ge.Props["Dimensions"]; }); return ge; }
//Create Track One public static GameEntity CreateTrackOne(int x, int y, int w, int h) { GameEntity ge = new GameEntity(); ge.Type = EntityType.WAVE_TRACK; CustomProperty dim = new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = new Rectangle(x, y, w, h) }; ge.Props.TryAdd(dim); CustomProperty osc = new CustomProperty { Name = "Oscillation", Type = typeof(float), DefaultValue = 1.0f }; ge.Props.TryAdd(osc); CustomProperty spd = new CustomProperty { Name = "Speed", Type = typeof(float), DefaultValue = 1.0f }; ge.Props.TryAdd(spd); CustomProperty img = new CustomProperty { Name = "Image", Type = typeof(Image), DefaultValue = null }; ge.Props.TryAdd(img); ge.SetBoundingBox = new delSetBoundingBox(delegate(Rectangle r) { ge.Props["Dimensions"] = r; }); ge.GetBoundingBox = new delGetBoundingBox(delegate() { return ge.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)ge.Props["Dimensions"]; }); return ge; }
//create sprite public static GameEntity CreateSprite(int x, int y, int w, int h) { GameEntity ge = new GameEntity(); ge.Type = EntityType.SPRITE; ge.Props.TryAdd(new CustomProperty { Name = "OutlineColor", Type = typeof(Color), DefaultValue = Color.Transparent }); ge.Props.TryAdd(new CustomProperty { Name = "Flipped", Type = typeof(bool), DefaultValue = false }); ge.Props.TryAdd(new CustomProperty { Name = "SpriteName", Type = typeof(string), DefaultValue = "water1.png" }); CustomProperty dim = new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = new Rectangle(x, y, w, h) }; ge.Props.TryAdd(dim); ge.SetBoundingBox = new delSetBoundingBox(delegate (Rectangle r) { ge.Props["Dimensions"] = r; }); ge.GetBoundingBox = new delGetBoundingBox(delegate () { return ge.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)ge.Props["Dimensions"]; }); return ge; }
//create linetrack public static GameEntity CreateLineTrack(int x, int y, int w, int h) { GameEntity ge = new GameEntity(); ge.Type = EntityType.LINETRACK; ge.Props.TryAdd(new CustomProperty { Name = "DispHeight", Type = typeof(int), DefaultValue = 10 }); ge.Props.TryAdd(new CustomProperty { Name = "PicWidth", Type = typeof(int), DefaultValue = 50 }); ge.Props.TryAdd(new CustomProperty { Name = "PicHeight", Type = typeof(int), DefaultValue = 50 }); ge.Props.TryAdd(new CustomProperty { Name = "Speed", Type = typeof(int), DefaultValue = 1 }); ge.Props.TryAdd(new CustomProperty { Name = "SpriteName", Type = typeof(string), DefaultValue = "water1.png" }); CustomProperty dim = new CustomProperty { Name = "Dimensions", Type = typeof(Rectangle), DefaultValue = new Rectangle(x, y, w, h) }; ge.Props.TryAdd(dim); ge.SetBoundingBox = new delSetBoundingBox(delegate(Rectangle r) { ge.Props["Dimensions"] = r; }); ge.GetBoundingBox = new delGetBoundingBox(delegate() { return ge.Props["Dimensions"] == null ? (Rectangle)dim.DefaultValue : (Rectangle)ge.Props["Dimensions"]; }); return ge; }
// Create Circle public static GameEntity CreateCircle(int radius, Point position) { GameEntity ge = new GameEntity(); ge.Type = EntityType.CIRCLE; ge.Props.TryAdd(new CustomProperty { Name = "OutlineColor", Type = typeof(Color), DefaultValue = Color.Magenta }); ge.Props.TryAdd(new CustomProperty { Name = "FillColor", Type = typeof(Color), DefaultValue = Color.Transparent }); CustomProperty rad = new CustomProperty { Name = "Radius", Type = typeof(int), DefaultValue = radius }; CustomProperty pos = new CustomProperty { Name = "Position", Type = typeof(Point), DefaultValue = new Point(position.X, position.Y) }; ge.Props.TryAdd(rad); ge.Props.TryAdd(pos); ge.SetBoundingBox = new delSetBoundingBox(delegate (Rectangle r) { // Must set bounding box based on the circles radius and location ge.Props["Radius"] = r.Width/2; ge.Props["Position"] = r.Location; }); ge.GetBoundingBox = new delGetBoundingBox(delegate () { Size sizeDefault = new Size((int)rad.DefaultValue * 2, (int)rad.DefaultValue * 2); Rectangle bb = new Rectangle ((Point)pos.DefaultValue, sizeDefault); if (!(ge.Props["Radius"] == null && ge.Props["Position"] == null)) { // Set bounding box to encompass circle based on diameter Size size = new Size((int)ge.Props["Radius"] * 2, (int)ge.Props["Radius"] * 2); bb = new Rectangle((Point)ge.Props["Position"], size); } return bb; }); return ge; }