Exemple #1
0
        public void writeElement(OBJECT _object)
        {
            mWriter.WriteStartElement("Object");
            VECTOR2D temp = new VECTOR2D();

            temp = Support.ConvertCoordination(_object);
            mWriter.WriteAttributeString("Type", Convert.ToString(_object.Type));
            mWriter.WriteAttributeString("Id", Convert.ToString(_object.ID));
            mWriter.WriteAttributeString("X", Convert.ToString(temp.cX));
            mWriter.WriteAttributeString("Y", Convert.ToString(temp.cY));
            if (_object.Type == 1)
            {
                mWriter.WriteAttributeString("Width", Convert.ToString(_object.Bound.width));
                mWriter.WriteAttributeString("Height", Convert.ToString(_object.Bound.height));
            }
            mWriter.Flush();
            mWriter.WriteEndElement();
        }
 public void writeElement(OBJECT _object)
 {
     mWriter.WriteStartElement("Object");
     VECTOR2D temp = new VECTOR2D();
     temp = Support.ConvertCoordination(_object);
     if (_object.ID == 26 && temp.cY == 32 && temp.cX == 32)
     {
         MessageBox.Show("DAY");
     }
     mWriter.WriteAttributeString("Type", Convert.ToString(_object.Type));
     mWriter.WriteAttributeString("Id", Convert.ToString(_object.ID));
     mWriter.WriteAttributeString("X", Convert.ToString(temp.cX));
     mWriter.WriteAttributeString("Y", Convert.ToString(temp.cY));
     if (_object.Type == 1)
     {
         mWriter.WriteAttributeString("Width", Convert.ToString(_object.Bound.width));
         mWriter.WriteAttributeString("Height", Convert.ToString(_object.Bound.height));
     }
     mWriter.Flush();
     mWriter.WriteEndElement();
 }
        public void InsertObject(CNode node, OBJECT obj)
        {
            RECTANGLE rect = RECTANGLE.Intersec(node.m_bound, obj.RangeOfMovement);

            if (rect.cX == 0 && rect.cY == 0 && rect.width == 0 && rect.height == 0)
            {
                return ;
            }

            if (node.m_bound.width >= (MAX_HEIGHT_SIZE_OF_NODE + 10) && node.m_bound.height > (MAX_HEIGHT_SIZE_OF_NODE + 10))
            {
                if (node.m_tl == null)
                {
                    node.m_tl = new CNode(node.m_id, PositionOfNode.TopLeft, node.Bound);
                    node.m_tr = new CNode(node.m_id, PositionOfNode.TopRight, node.Bound);
                    node.m_bl = new CNode(node.m_id, PositionOfNode.BottomLeft, node.Bound);
                    node.m_br = new CNode(node.m_id, PositionOfNode.BottomRight, node.Bound);
                }

                node.InsertObject(node.m_tl, obj);
                node.InsertObject(node.m_tr, obj);
                node.InsertObject(node.m_bl, obj);
                node.InsertObject(node.m_br, obj);
            }
            else
            {
                node.m_listObject.Add(obj);
            }

            return;
        }
        public void SaveMap()
        {
            if (Support.listObject != null && Support.IsExportXml == false)
            {
                for (int i = 0; i < WorkspaceWorking.Children.Count; ++i)
                {
                    if (WorkspaceWorking.Children[i] is Image)
                    {
                        Image tempRect = (Image)WorkspaceWorking.Children[i];
                        if ((int)tempRect.Tag != (int)ObjectType.TILE_MAP)
                        {
                            ++Support.Count;
                            OBJECT temp = new OBJECT((int)ObjectType.NORMAL_OBJECT, (int)tempRect.Tag, Support.Count, new VECTOR2D((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i])), new RECTANGLE((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i]), (int)tempRect.Width, (int)tempRect.Height));
                            VECTOR2D tempPosition = Support.ConvertCoordination(temp);
                            Support.listObject.Add(new OBJECT((int)ObjectType.NORMAL_OBJECT, (int)tempRect.Tag, Support.Count, tempPosition, new RECTANGLE(tempPosition.cX, tempPosition.cY, (int)tempRect.Width, (int)tempRect.Height)));
                        }
                    }
                    else if (WorkspaceWorking.Children[i] is Rectangle && Support.IsImport == true)
                    {
                        ++Support.Count;
                        Rectangle tempImage = (Rectangle)WorkspaceWorking.Children[i];

                        if (tempImage.Tag != Support.gridLine)
                        {
                            OBJECT temp = new OBJECT((int)ObjectType.VIRTUAL_OBJECT, (int)tempImage.Tag, Support.Count, new VECTOR2D((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i])), new RECTANGLE((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i]), (int)tempImage.Width, (int)tempImage.Height));
                            VECTOR2D tempPosition = Support.ConvertCoordination(temp);
                            Support.listObject.Add(new OBJECT((int)ObjectType.VIRTUAL_OBJECT, (int)tempImage.Tag, Support.Count, tempPosition, new RECTANGLE(tempPosition.cX, tempPosition.cY, (int)tempImage.Width, (int)tempImage.Height)));
                        }
                    }
                }

                Support.IsImport = false;

                if (Support.quadTree == null)
                {
                    Support.quadTree = new CNode(0, PositionOfNode.TopLeft, new RECTANGLE(0, 8192, 8192, 8192));
                    for (int i = 0; i < Support.listObject.Count; ++i)
                    {
                        Support.quadTree.InsertObject(Support.quadTree, Support.listObject[i]);
                    }
                }
                ExportXml.getInstance().MWriter.WriteStartDocument();
                ExportXml.getInstance().WriteInfomationForMap();
                ExportXml.getInstance().writeQuadtreeToXml(Support.quadTree, ExportXml.getInstance().MWriter);
                ExportXml.getInstance().MWriter.WriteEndDocument();
                ExportXml.getInstance().MWriter.Close();

                Support.IsExportXml = true;
                MessageBox.Show("Map đã được tạo thành công, dùng file map.xml để load game nhé ^^", "Thông báo");
            }
            else
            {
                MessageBox.Show("Có map đâu mà đòi lưu ! - Muốn lưu hãy tạo map mới ^^", "Thông báo");
            }
        }
        public void move_to_update_position(object sender, MouseEventArgs m)
        {
            if (isDragged == true && m.LeftButton == MouseButtonState.Pressed && (Support.IsVirtualObject == true || Support.IsJump == true || Support.IsVirtualWater == true))
            {
                if (rect != null)
                {
                    WorkspaceWorking.Children.Remove(rect);
                }

                endPosition = m.GetPosition(WorkspaceWorking);

                #region Scroll SrollViewer of Canvas
                if (endPosition.X > 800 + ScrollCanvas.HorizontalOffset)
                {
                    ScrollCanvas.ScrollToHorizontalOffset(ScrollCanvas.HorizontalOffset + 1);
                    ScrollCanvas.UpdateLayout();
                }
                #endregion

                endPosition.X = ((int)endPosition.X / Support.WIDHT_OF_VIRTUALOBJECT) * Support.WIDHT_OF_VIRTUALOBJECT;
                endPosition.Y = ((int)endPosition.Y / Support.HEIGHT_OF_VIRTUALOBJECT) * Support.HEIGHT_OF_VIRTUALOBJECT;

                if ((int)endPosition.X % Support.WIDHT_OF_VIRTUALOBJECT > 0 || (endPosition.X - startPosition.X) / Support.WIDHT_OF_VIRTUALOBJECT < 1)
                {
                    endPosition.X += Support.WIDHT_OF_VIRTUALOBJECT;
                }

                if ((int)endPosition.Y % Support.HEIGHT_OF_VIRTUALOBJECT > 0 || (endPosition.Y - startPosition.Y) / Support.HEIGHT_OF_VIRTUALOBJECT < 1)
                {
                    endPosition.Y += Support.HEIGHT_OF_VIRTUALOBJECT;
                }

                rect = new Rectangle();
                rect.Tag = Support.virtualObject;
                rect.Width = (double)Math.Abs(endPosition.X - startPosition.X);
                rect.Height = (double)Math.Abs(endPosition.Y - startPosition.Y);
                if (Support.IsVirtualWater == true)
                {
                    rect.Stroke = new SolidColorBrush(Colors.Blue);
                }
                else if(Support.IsJump == false)
                {
                    rect.Stroke = new SolidColorBrush(Colors.Red);
                }
                else if (Support.IsJump == true)
                {
                    rect.Stroke = new SolidColorBrush(Colors.Green);
                }
                rect.StrokeThickness = 3;

                if (endPosition.X > startPosition.X)
                {
                    Canvas.SetLeft(rect, startPosition.X);
                    finalPosition.X = startPosition.X;
                }
                else
                {
                    Canvas.SetLeft(rect, endPosition.X);
                    finalPosition.X = endPosition.X;
                }

                if (startPosition.Y < endPosition.Y)
                {
                    Canvas.SetTop(rect, startPosition.Y);
                    finalPosition.Y = startPosition.Y;
                }
                else
                {
                    Canvas.SetTop(rect, endPosition.Y);
                    finalPosition.Y = endPosition.Y;
                }
                Canvas.SetZIndex(rect, 2);
                WorkspaceWorking.Children.Add(rect);
            }
            else
            {
                if (isDragged == true && m.LeftButton == MouseButtonState.Released)
                {
                    isDragged = false;
                    if (rect != null)
                    {
                        ++Support.Count;
                        if (Support.IsVirtualWater == false && Support.IsJump == false)
                        {
                            OBJECT temp = new OBJECT((int)ObjectType.VIRTUAL_OBJECT, 0, Support.Count, new VECTOR2D((float)finalPosition.X, (float)finalPosition.Y), new RECTANGLE((float)finalPosition.X, (float)finalPosition.Y, (int)rect.Width, (int)rect.Height));
                            VECTOR2D tempPosition = Support.ConvertCoordination(temp);

                            OBJECT obj = new OBJECT((int)ObjectType.VIRTUAL_OBJECT, 0, Support.Count, new VECTOR2D((float)tempPosition.cX, (float)tempPosition.cY), new RECTANGLE((float)tempPosition.cX, (float)tempPosition.cY, (int)rect.Width, (int)rect.Height));

                            if (Support.listObject == null)
                            {
                                Support.listObject = new List<OBJECT>();
                            }
                            if (obj.Bound.width != 0 && obj.Bound.height != 0)
                            {
                                Support.listObject.Add(obj);
                            }

                            obj = null;

                        }
                        else if(Support.IsVirtualWater == true)
                        {
                            OBJECT temp = new OBJECT((int)ObjectType.VIRTUAL_OBJECT, 0, Support.Count, new VECTOR2D((float)finalPosition.X, (float)finalPosition.Y), new RECTANGLE((float)finalPosition.X, (float)finalPosition.Y, (int)rect.Width, (int)rect.Height));
                            VECTOR2D tempPosition = Support.ConvertCoordination(temp);

                            EditPosition form = new EditPosition((int)tempPosition.cX, (int)tempPosition.cY);
                            form.ShowDialog();
                            tempPosition = new VECTOR2D(EditPosition.finalLocationX, EditPosition.finalLocationY);

                            OBJECT obj = new OBJECT((int)ObjectType.VIRTUAL_OBJECT, (int)ObjectID.VIRTUAL_OBJECT_WATER, Support.Count, new VECTOR2D((float)tempPosition.cX, (float)tempPosition.cY), new RECTANGLE((float)tempPosition.cX, (float)tempPosition.cY, (int)rect.Width, (int)rect.Height));

                            if (Support.listObject == null)
                            {
                                Support.listObject = new List<OBJECT>();
                            }
                            if (obj.Bound.width != 0 && obj.Bound.height != 0)
                            {
                                Support.listObject.Add(obj);
                            }

                            obj = null;

                        }
                        else if (Support.IsJump == true)
                        {
                            OBJECT temp = new OBJECT((int)ObjectType.VIRTUAL_OBJECT, 0, Support.Count, new VECTOR2D((float)finalPosition.X, (float)finalPosition.Y), new RECTANGLE((float)finalPosition.X, (float)finalPosition.Y, (int)rect.Width, (int)rect.Height));
                            VECTOR2D tempPosition = Support.ConvertCoordination(temp);

                            EditPosition form = new EditPosition((int)tempPosition.cX, (int)tempPosition.cY);
                            form.ShowDialog();
                            tempPosition = new VECTOR2D(EditPosition.finalLocationX, EditPosition.finalLocationY);

                            OBJECT obj = new OBJECT((int)ObjectType.VIRTUAL_OBJECT, (int)ObjectID.VIRTUAL_OBJECT_JUMP, Support.Count, new VECTOR2D((float)tempPosition.cX, (float)tempPosition.cY), new RECTANGLE((float)tempPosition.cX, (float)tempPosition.cY, (int)rect.Width, (int)rect.Height));

                            if (Support.listObject == null)
                            {
                                Support.listObject = new List<OBJECT>();
                            }
                            if (obj.Bound.width != 0 && obj.Bound.height != 0)
                            {
                                Support.listObject.Add(obj);
                            }

                            obj = null;
                        }
                    }
                    rect = null;
                    startPosition = endPosition;
                }
            }

            // Update coordination for tb
            if (this.tbX != null && this.tbY != null)
            {
                this.tbX.Text = m.GetPosition(WorkspaceWorking).X.ToString();
                this.tbY.Text = (Support.HEIGHT_MAP - m.GetPosition(WorkspaceWorking).Y).ToString();
            }

            if (Support.IsEraser == false)
            {
                #region.For to create enemyobject

                if (selectedItemFromListbox != null && m.LeftButton != MouseButtonState.Pressed)
                {
                    isAdded = false;
                    WorkspaceWorking.Children.Remove(tempSelectedItem);

                    Image cursorImage = new Image();
                    cursorImage.Tag = selectedItemFromListbox.Tag;
                    cursorImage.Source = selectedItemFromListbox.Source;
                    cursorImage.Width = selectedItemFromListbox.Width;
                    cursorImage.Height = selectedItemFromListbox.Height;
                    cursorImage.Opacity = 0.8f;

                    positionTempOfSelectedFromListbox = m.GetPosition(WorkspaceWorking);

                    tempSelectedItem = cursorImage;

                    if (Support.IsAlign == true)
                    {
                        #region.Collision between rambo & virtualobject
                        for (int i = 0; i < WorkspaceWorking.Children.Count; ++i)
                        {
                            RECTANGLE cursor = new RECTANGLE((float)positionTempOfSelectedFromListbox.X, (float)positionTempOfSelectedFromListbox.Y, (int)cursorImage.Width, (int)cursorImage.Height);

                            if (WorkspaceWorking.Children[i] is Rectangle)
                            {
                                Rectangle convertingObject = (Rectangle)(WorkspaceWorking.Children[i]);
                                if (convertingObject.Tag == Support.virtualObject && ((int)cursorImage.Tag == (int)ObjectID.RAMBO ||
                                                                (int)cursorImage.Tag == (int)ObjectID.SNIPER_STANDING) ||  (int)cursorImage.Tag == (int)ObjectID.TANK
                                ||  (int)cursorImage.Tag == (int)ObjectID.ENEMY_BIG_GUN_SHOOTING ||  (int)cursorImage.Tag == (int)ObjectID.ENEMY_RUN)
                                {
                                    RECTANGLE tempRect = new RECTANGLE((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i]), (int)convertingObject.Width, (int)convertingObject.Height);
                                    RECTANGLE collisionRect = RECTANGLE.IntersectCanvas(tempRect, cursor);
                                    if (collisionRect.width != 0 && collisionRect.height != 0)
                                    {
                                        positionTempOfSelectedFromListbox.Y = tempRect.cY - cursorImage.Height;
                                    }
                                }
                                else if (convertingObject.Tag == Support.gridLine && ((int)cursorImage.Tag == (int)ObjectID.GUN_ROTATING ||
                                                                     (int)cursorImage.Tag == (int)ObjectID.BIG_GUN_ROTATING) || (int)cursorImage.Tag == (int)ObjectID.BRIDGE
                                    || (int)cursorImage.Tag == (int)ObjectID.WEAPON_SENSOR || (int)cursorImage.Tag == (int)ObjectID.FIRE_BRIDGE || (int)cursorImage.Tag == (int)ObjectID.BIG_BOSS_1 || (int)cursorImage.Tag == (int)ObjectID.BIG_CAPSULE_BOSS)
                                {
                                    RECTANGLE tempRect = new RECTANGLE((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i]), (int)convertingObject.Width, (int)convertingObject.Height);
                                    RECTANGLE collisionRect = RECTANGLE.IntersectCanvas(tempRect, cursor);
                                    if (collisionRect.width != 0 && collisionRect.height != 0)
                                    {
                                        positionTempOfSelectedFromListbox.X = tempRect.cX;
                                        positionTempOfSelectedFromListbox.Y = tempRect.cY;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    Canvas.SetLeft(cursorImage, (double)(positionTempOfSelectedFromListbox.X));
                    Canvas.SetTop(cursorImage, (double)(positionTempOfSelectedFromListbox.Y));
                    WorkspaceWorking.Children.Add(cursorImage);
                        #endregion
                }

                if (m.LeftButton == MouseButtonState.Pressed && selectedItemFromListbox != null && isAdded == false)
                {
                    WorkspaceWorking.Children.Remove(tempSelectedItem);
                    Image finalSelectedItem = new Image();
                    finalSelectedItem.Tag = selectedItemFromListbox.Tag;
                    finalSelectedItem.Source = selectedItemFromListbox.Source;
                    finalSelectedItem.Width = selectedItemFromListbox.Width;
                    finalSelectedItem.Height = selectedItemFromListbox.Height;

                    Canvas.SetLeft(finalSelectedItem, (double)(positionTempOfSelectedFromListbox.X));
                    Canvas.SetTop(finalSelectedItem, (double)(positionTempOfSelectedFromListbox.Y));
                    WorkspaceWorking.Children.Add(finalSelectedItem);
                    isAdded = true;

                    if ((int)finalSelectedItem.Tag == (int)ObjectID.WEAPON_CAPSULE || (int)finalSelectedItem.Tag == (int)ObjectID.WEAPON_SENSOR)
                    {
                        ItemList itemList = new ItemList(WorkspaceWorking, positionTempOfSelectedFromListbox);
                        itemList.ShowDialog();
                    }
                }
            }
            else
            {
                for (int i = 0; i < WorkspaceWorking.Children.Count; ++i)
                {
                    if (WorkspaceWorking.Children[i] is Image)
                    {
                        Image tempSelected = (Image)(WorkspaceWorking.Children[i]);
                        if ((int)tempSelected.Tag != (int)ObjectType.TILE_MAP)
                        {
                            RECTANGLE rect = new RECTANGLE((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i]), (int)tempSelected.Width, (int)tempSelected.Height);
                            System.Windows.Point tempPoint = new System.Windows.Point(m.GetPosition(WorkspaceWorking).X, m.GetPosition(WorkspaceWorking).Y);
                            if (rect.IsContain(tempPoint) == true)
                            {
                                WorkspaceWorking.Children.Remove(WorkspaceWorking.Children[i]);
                            }
                        }
                    }
                    else if (WorkspaceWorking.Children[i] is Rectangle)
                    {
                        Rectangle tempRect = (Rectangle)(WorkspaceWorking.Children[i]);
                        if (tempRect.Tag == Support.virtualObject && tempRect.Tag != Support.gridLine)
                        {
                            RECTANGLE rect = new RECTANGLE((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i]), (int)tempRect.Width, (int)tempRect.Height);
                            System.Windows.Point tempPoint = new System.Windows.Point(m.GetPosition(WorkspaceWorking).X, m.GetPosition(WorkspaceWorking).Y);
                            if (rect.IsContain(tempPoint) == true)
                            {
                                WorkspaceWorking.Children.Remove(WorkspaceWorking.Children[i]);
                            }
                        }
                    }
                }
            }
            #endregion
        }
 public void ConvertToCanvasForFileExisted()
 {
     for (int i = 0; i < WorkspaceWorking.Children.Count; ++i)
     {
         if (WorkspaceWorking.Children[i] is Rectangle)
         {
             Rectangle tempRect = (Rectangle)WorkspaceWorking.Children[i];
             if ((int)tempRect.Tag == (int)ObjectID.TILE_BASE)
             {
                 ++Support.Count;
                 OBJECT temp = new OBJECT((int)ObjectType.VIRTUAL_OBJECT, (int)tempRect.Tag, Support.Count, new VECTOR2D((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i])), new RECTANGLE((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i]), (int)tempRect.Width, (int)tempRect.Height));
                 VECTOR2D tempPosition = Support.ConvertCoordination(temp);
                 Support.listObject.Add(new OBJECT((int)ObjectType.VIRTUAL_OBJECT, (int)tempRect.Tag, Support.Count, tempPosition, new RECTANGLE(tempPosition.cX, tempPosition.cY, (int)tempRect.Width, (int)tempRect.Height)));
             }
         }
     }
 }
        public bool ConvertFromTileToObject()
        {
            if (Support.map != null)
            {
                if (Support.listObject == null)
                {
                    Support.listObject = new List<OBJECT>();
                }
                else if(Support.listObject.Count != 0)
                {
                    Support.listObject.Clear();
                }

                for (int i = 0; i < (int)(Support.map.BitMap.PixelHeight)/ Support.HEIGHT_OF_TILE; ++i)
                {
                    for (int j = 0; j < (int)(Support.map.BitMap.PixelWidth)/ Support.WIDTH_OF_TILE; ++j)
                    {
                        if (Support.listObject != null)
                        {
                            ++Support.Count;
                            OBJECT tempObject = new OBJECT((int)ObjectType.TILE_MAP, Support.map.ArrMap[i, j], Support.Count, new VECTOR2D(j * Support.WIDTH_OF_TILE, i * Support.HEIGHT_OF_TILE), new RECTANGLE(j * Support.WIDTH_OF_TILE, i * Support.HEIGHT_OF_TILE, Support.WIDTH_OF_TILE, Support.HEIGHT_OF_TILE));
                            VECTOR2D tempPosition = Support.ConvertCoordination(tempObject);
                            Support.listObject.Add(new OBJECT((int)ObjectType.TILE_MAP, Support.map.ArrMap[i, j], Support.Count, tempPosition, new RECTANGLE(tempPosition.cX, tempPosition.cY, Support.WIDTH_OF_TILE, Support.HEIGHT_OF_TILE)));
                        }
                    }
                }

                for (int i = 0; i < Support.map.ListLedPosition.Count; ++i)
                {
                    ++Support.Count;
                    OBJECT tempObject = new OBJECT((int)ObjectType.LED_OBJECT, 0, Support.Count, new VECTOR2D(Support.map.ListLedPosition[i].cX, Support.map.ListLedPosition[i].cY), new RECTANGLE(Support.map.ListLedPosition[i].cX, Support.map.ListLedPosition[i].cY, 2, 2));
                    VECTOR2D position = Support.ConvertCoordination(tempObject);
                    Support.listObject.Add(new OBJECT((int)ObjectType.LED_OBJECT, 0, Support.Count, new VECTOR2D(position.cX, position.cY), new RECTANGLE(position.cX, position.cY, 2, 2)));
                }

                return true;
            }

            return false;
        }
        public static VECTOR2D ConvertCoordination(OBJECT _object)
        {
            VECTOR2D temp = new VECTOR2D(_object.Position.cX, _object.Position.cY);
            temp.cY = (int)(Support.HEIGHT_MAP - _object.Position.cY);
            temp.cX = (int)(temp.cX + _object.Bound.width / 2);
            temp.cY = (int)(temp.cY - _object.Bound.height / 2);

            return temp;
        }