Exemple #1
0
        protected void AddCollisionPoints(ref World world, ref MapInterface.ObjectTemplate temp, BodyDef bodyDef, PolygonDef polyDef, List <prim.Point> points, ref Body body)
        {
            int vCnt = -1;

            points = GetDistinctPoints(points);
            foreach (prim.Point curPoint in points)
            {
                vCnt += 1;
                polyDef.Vertices[vCnt].Set(curPoint.x, curPoint.y);
            }

            bodyDef.FixedRotation = true;
            body = world.CreateBody(bodyDef);
            body.CreateShape(polyDef);
            //points = GetDistinctPoints(points);
            //int vCnt = -1;
            //foreach (prim.Point curPoint in points)
            //{
            //    vCnt += 1;
            //    polyDef.Vertices[vCnt].Set(curPoint.x, curPoint.y);
            //}
            //bodyDef.FixedRotation = true;
            //body = world.CreateBody(bodyDef);
            //body.CreateShape(polyDef);
            //if (!temp.isSoft)
            //  body.SetMassFromShapes();
        }
 public ObjectTemplateForm(MapInterface.MapInterface mapInterface)
 {
     InitializeComponent();
     this.mapInterface = mapInterface;
     objTemplate       = new MapInterface.ObjectTemplate();
     updatePropertyList();
     UpdateImageListView();
 }
Exemple #3
0
 public ControlEntity(World world, MapInterface.ObjectTemplate template, gfx.Graphics graphics)
 {
     this.active   = true;
     this.graphics = graphics;
     controlObject = new ObjectEntity(world, template, graphics, new prim.Point(0f, 0f), true);
     vx            = 0.5f;
     vy            = 0.5f;
 }
 public ObjectTemplateForm(MapInterface.MapInterface mapInterface, string key)
 {
     InitializeComponent();
     this.mapInterface = mapInterface;
     objTemplate       = this.mapInterface.objectTemplates[key];
     updatePropertyList();
     UpdateImageListView();
     this.objNameText.Text = key;
 }
 public void AddObjectTemplate(ObjectTemplate objTemplate)
 {
     if (objectTemplates.ContainsKey(objTemplate.name))
     {
         objectTemplates[objTemplate.name] = objTemplate;
     }
     else
     {
         objectTemplates.Add(objTemplate.name, objTemplate);
     }
 }
 public ObjectTemplate(ObjectTemplate objTemplate)
 {
     this.images     = objTemplate.images;
     this.properties = new Dictionary <string, Property>();
     foreach (KeyValuePair <string, Property> keyVal in objTemplate.properties)
     {
         this.properties.Add(keyVal.Key, new Property(keyVal.Value));
     }
     this.name       = objTemplate.name;
     this.visibility = objTemplate.visibility;
     this.isSoft     = objTemplate.isSoft;
 }
Exemple #7
0
 public ObjectEntity(World world, MapInterface.ObjectTemplate temp, Graphics graphics, prim.Point point, bool isDynamic = false) : base()
 {
     this.point       = point;
     this.x           = point.x;
     this.y           = point.y;
     this.dx          = 0f;
     this.dy          = 0f;
     this.colEntities = new List <CollisionEntity>();
     this.instance    = temp;
     objectStrategy   = new ObjectStrategy();
     objectStrategy.InitTexture(ref world, ref body, this.point, ref this.size, ref texture, ref this.instance, graphics, isDynamic);
     //this.InitTexture(world, temp, graphics, isDynamic);
 }
        public void NewMap(string path)
        {
            ObjectTemplate globalObject = new ObjectTemplate("Global");

            globalObject.SetDefaultImage();

            Property gravity = new Property("Gravity", "float", new byte[] { });

            gravity.SetFloat(0.0f);

            Property background = new Property("Background", "image", new byte[] { });
            Bitmap   defBg      = new Bitmap(200, 200);

            FillBitmap(defBg);
            background.SetImage(defBg);

            globalObject.AddProperty(gravity);
            globalObject.AddProperty(background);

            this.AddObjectTemplate(globalObject);

            this.Save(path);
        }
 public ObjectWall(ObjectTemplate objTemp) : base(objTemp)
 {
     w = 0;
     h = 0;
 }
 public ObjectInstance(ObjectTemplate objTemp)
 {
     instance = new ObjectTemplate(objTemp);
     x        = 0;
     y        = 0;
 }
 public ObjectInstance()
 {
     instance = new ObjectTemplate();
     x        = 0;
     y        = 0;
 }
        public void Save()
        {
            //TODO: Temp backup folder
            FileStream   fs = File.Create(this.path);
            BinaryWriter bw = new BinaryWriter(fs);

            //Number of Objects
            bw.Write(BitConverter.GetBytes(objectTemplates.Keys.Count));
            foreach (string key in objectTemplates.Keys)
            {
                ObjectTemplate curTemplate = objectTemplates[key];
                //Object Name
                bw.Write(getStringBuffer(curTemplate.name, 20));
                //Visibility
                bw.Write(getBoolBuffer(curTemplate.visibility));
                //Soft
                bw.Write(getBoolBuffer(curTemplate.isSoft));
                //Number of Properties
                bw.Write(BitConverter.GetBytes(curTemplate.properties.Keys.Count));
                //For each property
                foreach (string pKey in curTemplate.properties.Keys)
                {
                    Property curProperty = curTemplate.properties[pKey];
                    string   type        = curProperty.type;
                    int      size        = curProperty.getSize();
                    //Property name
                    bw.Write(getStringBuffer(curProperty.name, 20));
                    //Property type
                    bw.Write(getStringBuffer(curProperty.type, 20));
                    //Property size
                    bw.Write(BitConverter.GetBytes(size));
                    //Property value
                    bw.Write(curProperty.value);
                }
                //Number of images
                bw.Write(getIntBuffer(curTemplate.images.Count));
                foreach (string iKey in curTemplate.images.Keys)
                {
                    //Number of steps
                    bw.Write(getIntBuffer(curTemplate.images[iKey].Count));
                    foreach (ImageData imageData in curTemplate.images[iKey])
                    {
                        ImageData curImage = imageData;
                        //Bitmap name
                        bw.Write(getStringBuffer(iKey, 20));
                        //Bitmap size
                        int size = (int)getBitmapSize(curImage.image);
                        bw.Write(getIntBuffer(size));
                        //Bitmap value
                        bw.Write(getBitmapBytes(curImage.image));
                        //# of Collision Vectors
                        bw.Write(getIntBuffer(curImage.collisionVectors.Count));
                        foreach (Line curLine in curImage.collisionVectors)
                        {
                            //X1
                            bw.Write(getIntBuffer(curLine.x1));
                            //Y1
                            bw.Write(getIntBuffer(curLine.y1));
                            //X2
                            bw.Write(getIntBuffer(curLine.x2));
                            //Y2
                            bw.Write(getIntBuffer(curLine.y2));
                        }
                    }
                }
            }
            //Number of instances
            bw.Write(getIntBuffer(objectInstances.Count));
            foreach (ObjectInstance instance in objectInstances)
            {
                //Object Name
                bw.Write(getStringBuffer(instance.instance.name, 20));
                //X Value
                bw.Write(getFloatBuffer(instance.x));
                //Y Value
                bw.Write(getFloatBuffer(instance.y));
                ObjectTemplate newTemplate = objectTemplates[instance.instance.name];
                foreach (Property curProperty in instance.instance.properties.Values)
                {
                    string type = curProperty.type;
                    int    size = curProperty.getSize();
                    //Property size
                    bw.Write(BitConverter.GetBytes(size));
                    //Property value
                    bw.Write(curProperty.value);
                }
            }
            //Number of Walls
            bw.Write(getIntBuffer(objectWalls.Count));
            foreach (ObjectWall instance in objectWalls)
            {
                //Object Name
                bw.Write(getStringBuffer(instance.instance.name, 20));
                //X Value
                bw.Write(getFloatBuffer(instance.x));
                //Y Value
                bw.Write(getFloatBuffer(instance.y));
                //W Value
                bw.Write(getFloatBuffer(instance.w));
                //H Value
                bw.Write(getFloatBuffer(instance.h));
                ObjectTemplate newTemplate = objectTemplates[instance.instance.name];
                foreach (Property curProperty in instance.instance.properties.Values)
                {
                    string type = curProperty.type;
                    int    size = curProperty.getSize();
                    //Property size
                    bw.Write(BitConverter.GetBytes(size));
                    //Property value
                    bw.Write(curProperty.value);
                }
            }
            bw.Close();
        }
        public void Load()
        {
            try
            {
                Clear();
                Byte[] mainBuffer = File.ReadAllBytes(this.path);
                int    counter    = 0;
                //Number of objects
                int numObjs = traverseToInt(mainBuffer, ref counter);
                for (int i = 0; i < numObjs; i++)
                {
                    //Object name
                    string         objName     = traverseToString(mainBuffer, ref counter, 20);
                    ObjectTemplate curTemplate = new ObjectTemplate(objName);
                    //Visible
                    curTemplate.visibility = traverseToBool(mainBuffer, ref counter);
                    //Soft
                    curTemplate.isSoft = traverseToBool(mainBuffer, ref counter);
                    //Number of properties
                    int numProps = traverseToInt(mainBuffer, ref counter);
                    for (int j = 0; j < numProps; j++)
                    {
                        //Property name
                        string propertyName = traverseToString(mainBuffer, ref counter, 20);
                        //Property type
                        string propertyType = traverseToString(mainBuffer, ref counter, 20);
                        //Property size
                        int size = traverseToInt(mainBuffer, ref counter);
                        //Property value
                        byte[] value = traverseToByte(mainBuffer, ref counter, size);

                        curTemplate.AddProperty(propertyName, propertyType, value);
                    }
                    //Number of images
                    int numImages = traverseToInt(mainBuffer, ref counter);
                    for (int k = 0; k < numImages; k++)
                    {
                        int numSteps = traverseToInt(mainBuffer, ref counter);
                        for (int p = 0; p < numSteps; p++)
                        {
                            //Image name
                            string imageName = traverseToString(mainBuffer, ref counter, 20);
                            //Image size
                            int imageSize = traverseToInt(mainBuffer, ref counter);
                            //Image value
                            Bitmap image = traverseToBitmap(mainBuffer, ref counter, imageSize);
                            //# of Collision Vectors
                            int         nVect   = traverseToInt(mainBuffer, ref counter);
                            List <Line> vectors = new List <Line>();
                            for (int m = 0; m < nVect; m++)
                            {
                                int  x1   = traverseToInt(mainBuffer, ref counter);
                                int  y1   = traverseToInt(mainBuffer, ref counter);
                                int  x2   = traverseToInt(mainBuffer, ref counter);
                                int  y2   = traverseToInt(mainBuffer, ref counter);
                                Line line = new Line(x1, y1, x2, y2);
                                vectors.Add(line);
                            }
                            curTemplate.AddImage(imageName, image, vectors);
                        }
                    }
                    objectTemplates.Add(objName, curTemplate);
                }
                //Instance count
                int numInst = traverseToInt(mainBuffer, ref counter);
                for (int i = 0; i < numInst; i++)
                {
                    //Instance name
                    string         instName = traverseToString(mainBuffer, ref counter, 20);
                    ObjectInstance instance = new ObjectInstance(objectTemplates[instName]);
                    //X
                    instance.x = traverseToFloat(mainBuffer, ref counter);
                    //Y
                    instance.y = traverseToFloat(mainBuffer, ref counter);
                    foreach (Property curProperty in instance.instance.properties.Values)
                    {
                        //Size
                        int propSize = traverseToInt(mainBuffer, ref counter);
                        //Value
                        curProperty.value = traverseToByte(mainBuffer, ref counter, propSize);
                        instance.instance.AddProperty(curProperty.name, curProperty.type, curProperty.value);
                    }
                    objectInstances.Add(instance);
                }
                //Wall Instance count
                int numWallInst = traverseToInt(mainBuffer, ref counter);
                for (int i = 0; i < numWallInst; i++)
                {
                    //Instance name
                    string     instName = traverseToString(mainBuffer, ref counter, 20);
                    ObjectWall instance = new ObjectWall(objectTemplates[instName]);
                    //X
                    instance.x = traverseToFloat(mainBuffer, ref counter);
                    //Y
                    instance.y = traverseToFloat(mainBuffer, ref counter);
                    //W
                    instance.w = traverseToFloat(mainBuffer, ref counter);
                    //H
                    instance.w = traverseToFloat(mainBuffer, ref counter);
                    foreach (Property curProperty in instance.instance.properties.Values)
                    {
                        //Size
                        int propSize = traverseToInt(mainBuffer, ref counter);
                        //Value
                        curProperty.value = traverseToByte(mainBuffer, ref counter, propSize);
                        instance.instance.AddProperty(curProperty.name, curProperty.type, curProperty.value);
                    }
                    objectInstances.Add(instance);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Warrning: Not all error file loaded");
            }
        }
Exemple #14
0
        public override void InitTexture(ref World world, ref Body body, prim.Point point, ref prim.Size size, ref ImageTexture texture, ref MapInterface.ObjectTemplate temp, Graphics graphics, bool isDynamic = false)
        {
            Bitmap bitmap = temp.images["default"][0].image;

            SetPoints(graphics, bitmap, ref texture, point, ref size);
            BodyDef    bodyDef = new BodyDef();
            PolygonDef polyBox = new PolygonDef();
            PolygonDef polyDef = new PolygonDef();

            bodyDef.Position.Set(point.x, point.y);
            polyDef.Density     = 1f;
            polyBox.Density     = 1f;
            bodyDef.Angle       = 0f;
            polyDef.VertexCount = temp.images["default"][0].collisionVectors.Count;
            polyBox.SetAsBox(size.w / 2, size.h / 2);
            List <prim.Point> points = new List <prim.Point>();

            foreach (MapInterface.Line line in temp.images["default"][0].collisionVectors)
            {
                float nx1 = ((float)line.x1 / bitmap.Width) * (float)size.w;
                float nx2 = ((float)line.x2 / bitmap.Width) * (float)size.w;
                float ny1 = ((float)line.y1 / bitmap.Height) * (float)size.h;
                float ny2 = ((float)line.y2 / bitmap.Height) * (float)size.h;
                points.Add(new prim.Point(nx1, ny1));
                points.Add(new prim.Point(nx2, ny2));
            }

            AddCollisionPoints(ref world, ref temp, bodyDef, polyDef, points, ref body);

            if (isDynamic)
            {
                body.SetMassFromShapes();
            }
        }
Exemple #15
0
        public virtual void InitTexture(ref World world, ref Body body, prim.Point point, ref prim.Size size, ref ImageTexture texture, ref MapInterface.ObjectTemplate temp, Graphics graphics, bool isDynamic = false)
        {
            Bitmap bitmap = temp.images["default"][0].image;

            SetPoints(graphics, bitmap, ref texture, point, ref size);

            BodyDef    bodyDef = new BodyDef();
            PolygonDef polyBox = new PolygonDef();
            PolygonDef polyDef = new PolygonDef();

            bodyDef.Position.Set(point.x, point.y);
            polyDef.Density     = 1f;
            polyBox.Density     = 1f;
            bodyDef.Angle       = 0f;
            polyDef.VertexCount = temp.images["default"][0].collisionVectors.Count;
            polyBox.SetAsBox(size.w / 2, size.h / 2);
            List <prim.Point> points = new List <prim.Point>();

            foreach (MapInterface.Line line in temp.images["default"][0].collisionVectors)
            {
                float nx1 = ((float)line.x1 / bitmap.Width) * (float)size.w;
                float nx2 = ((float)line.x2 / bitmap.Width) * (float)size.w;
                float ny1 = ((float)line.y1 / bitmap.Height) * (float)size.h;
                float ny2 = ((float)line.y2 / bitmap.Height) * (float)size.h;
                points.Add(new prim.Point(nx1, ny1));
                points.Add(new prim.Point(nx2, ny2));
            }
            //TODO: Sort vertices in CCW order
            //Example. Top triangle point -> bottom right -> bottem left
            //points = GetDistinctPoints(points);
            //foreach (prim.Point curPoint in points)
            //{
            //    vCnt += 1;
            //    polyDef.Vertices[vCnt].Set(curPoint.x, curPoint.y);
            //}

            //bodyDef.FixedRotation = true;
            //body = world.CreateBody(bodyDef);
            //body.CreateShape(polyDef);

            AddCollisionPoints(ref world, ref temp, bodyDef, polyDef, points, ref body);

            if (isDynamic)
            {
                body.SetMassFromShapes();
            }
        }
Exemple #16
0
 public void AddControlEntity(Graphics graphics, string templateKey)
 {
     MapInterface.ObjectTemplate template = mapInterface.objectTemplates[templateKey];
     controlEntity = new ControlEntity(world, template, graphics);
 }