//Xác định xem _object.oRect va chạm với QNode nào, //thêm _object vào node mà nó va chạm public void AddObjectInNode(CTreeObject _object) { int index = -1; int sw = nRect.X + nRect.Width / 2; int sh = nRect.Y + nRect.Height / 2; bool top=(_object.oRect.Y+_object.oRect.Height <= sh);//hoàn toàn ở trên bool bottom = _object.oRect.Y >= sh; bool left = _object.oRect.X + _object.oRect.Width <= sw; bool right = _object.oRect.X >= sw; if (top && left) Node[0].Insert(_object); else if (top && right) Node[1].Insert(_object); else if (bottom && left) Node[2].Insert(_object); else if (bottom && right) Node[3].Insert(_object); else if (top && !left && !right) { Node[0].Insert(_object); Node[1].Insert(_object); } else if (bottom && !right && !left) { Node[2].Insert(_object); Node[3].Insert(_object); } else if (left && !top && !bottom) { Node[2].Insert(_object); Node[0].Insert(_object); } else if (right && !top && !bottom) { Node[1].Insert(_object); Node[3].Insert(_object); } else if (!right && !top && !left && !bottom) { Node[1].Insert(_object); Node[3].Insert(_object); Node[2].Insert(_object); Node[0].Insert(_object); } }
private void pbImgMap_MouseMove(object sender, MouseEventArgs e) { if (ctype == typeO._null || mousedown == 0 || remove) return; int i = e.X / 16; int j = e.Y / 16; int index = j * colum_Count + i; if (index < 0 || index > row_Count * colum_Count)//Kiểm tra phần tử có chỉ số index có tồn tại hay không return; //Thêm đối tượng vào map if (lCTreeObject[index].id == -1)//Kiểm tra tại vị trí đang xét đã có hay chưa, có rồi thì thôi { lCTreeObject[index].id = index; lCTreeObject[index].gameObject.id = index; } else return; GameObject t = new GameObject(index, ctype); t.Image = pImageType.Image; t.Size = new System.Drawing.Size(t.Image.Width, t.Image.Height); t.Location = new Point(i * 16, j * 16); Rectangle r = new Rectangle(i * 16, j * 16, t.Image.Width, t.Image.Height); CTreeObject ct = new CTreeObject(index, r, t); ct.gameObject.MouseDown += new MouseEventHandler(cTreeObject_MouseDown); switch (ctype) { case typeO.brick: c_brick.Add(ct); pbImgMap.Controls.Add(c_brick[c_brick.Count - 1].gameObject); c_brick[c_brick.Count - 1].gameObject.BringToFront(); lCtree.Add(c_brick[c_brick.Count - 1]); break; case typeO.Music: c_music.Add(ct); pbImgMap.Controls.Add(c_music[c_music.Count - 1].gameObject); c_music[c_music.Count - 1].gameObject.BringToFront(); lCtree.Add(c_music[c_music.Count - 1]); break; case typeO.coin: c_coin.Add(ct); pbImgMap.Controls.Add(c_coin[c_coin.Count - 1].gameObject); c_coin[c_coin.Count - 1].gameObject.BringToFront(); lCtree.Add(c_coin[c_coin.Count - 1]); break; case typeO.drain: c_drain.Add(ct); pbImgMap.Controls.Add(c_drain[c_drain.Count - 1].gameObject); c_drain[c_drain.Count - 1].gameObject.BringToFront(); lCtree.Add(c_drain[c_drain.Count - 1]); break; /*case typeO.land: c_land.Add(ct); pbImgMap.Controls.Add(c_land[c_land.Count - 1].gameObject); c_land[c_land.Count - 1].gameObject.BringToFront(); lCtree.Add(c_land[c_land.Count - 1]); break;*/ case typeO.leaf: c_leaf.Add(ct); pbImgMap.Controls.Add(c_leaf[c_leaf.Count - 1].gameObject); c_leaf[c_leaf.Count - 1].gameObject.BringToFront(); lCtree.Add(c_leaf[c_leaf.Count - 1]); break; case typeO.mushroom_red: c_mushroom_red.Add(ct); pbImgMap.Controls.Add(c_mushroom_red[c_mushroom_red.Count - 1].gameObject); c_mushroom_red[c_mushroom_red.Count - 1].gameObject.BringToFront(); lCtree.Add(c_mushroom_red[c_mushroom_red.Count - 1]); break; case typeO.p: c_p.Add(ct); pbImgMap.Controls.Add(c_p[c_p.Count - 1].gameObject); c_p[c_p.Count - 1].gameObject.BringToFront(); lCtree.Add(c_p[c_p.Count - 1]); break; /*case typeO.Box: c_box.Add(ct); pbImgMap.Controls.Add(c_box[c_box.Count - 1].gameObject); c_box[c_box.Count - 1].gameObject.BringToFront(); lCtree.Add(c_box[c_box.Count - 1]); break;*/ case typeO.question_block: c_question_block.Add(ct); pbImgMap.Controls.Add(c_question_block[c_question_block.Count - 1].gameObject); c_question_block[c_question_block.Count - 1].gameObject.BringToFront(); lCtree.Add(c_question_block[c_question_block.Count - 1]); break; case typeO.star: c_star.Add(ct); pbImgMap.Controls.Add(c_star[c_star.Count - 1].gameObject); c_star[c_star.Count - 1].gameObject.BringToFront(); lCtree.Add(c_star[c_star.Count - 1]); break; case typeO.tarnooki: c_tarnooki.Add(ct); pbImgMap.Controls.Add(c_tarnooki[c_tarnooki.Count - 1].gameObject); c_tarnooki[c_tarnooki.Count - 1].gameObject.BringToFront(); lCtree.Add(c_tarnooki[c_tarnooki.Count - 1]); break; case typeO.tarnooki_fly: c_tarnooki_fly.Add(ct); pbImgMap.Controls.Add(c_tarnooki_fly[c_tarnooki_fly.Count - 1].gameObject); c_tarnooki_fly[c_tarnooki_fly.Count - 1].gameObject.BringToFront(); lCtree.Add(c_tarnooki_fly[c_tarnooki_fly.Count - 1]); break; case typeO.tortoise: c_tortoise.Add(ct); pbImgMap.Controls.Add(c_tortoise[c_tortoise.Count - 1].gameObject); c_tortoise[c_tortoise.Count - 1].gameObject.BringToFront(); lCtree.Add(c_tortoise[c_tortoise.Count - 1]); break; case typeO.tortoise_fly: c_tortoise_fly.Add(ct); pbImgMap.Controls.Add(c_tortoise_fly[c_tortoise_fly.Count - 1].gameObject); c_tortoise_fly[c_tortoise_fly.Count - 1].gameObject.BringToFront(); lCtree.Add(c_tortoise_fly[c_tortoise_fly.Count - 1]); break; case typeO.tortoise_red: c_tortoise_red.Add(ct); pbImgMap.Controls.Add(c_tortoise_red[c_tortoise_red.Count - 1].gameObject); c_tortoise_red[c_tortoise_red.Count - 1].gameObject.BringToFront(); lCtree.Add(c_tortoise_red[c_tortoise_red.Count - 1]); break; case typeO.tree: c_tree.Add(ct); pbImgMap.Controls.Add(c_tree[c_tree.Count - 1].gameObject); c_tree[c_tree.Count - 1].gameObject.BringToFront(); lCtree.Add(c_tree[c_tree.Count - 1]); break; case typeO.tree_red: c_tree_red.Add(ct); pbImgMap.Controls.Add(c_tree_red[c_tree_red.Count - 1].gameObject); c_tree_red[c_tree_red.Count - 1].gameObject.BringToFront(); lCtree.Add(c_tree_red[c_tree_red.Count - 1]); break; case typeO.tree_red_shoot: c_tree_red_shoot.Add(ct); pbImgMap.Controls.Add(c_tree_red_shoot[c_tree_red_shoot.Count - 1].gameObject); c_tree_red_shoot[c_tree_red_shoot.Count - 1].gameObject.BringToFront(); lCtree.Add(c_tree_red_shoot[c_tree_red_shoot.Count - 1]); break; //case typeO.tree_shoot: // c_tree_shoot.Add(ct); // pbImgMap.Controls.Add(c_tree_shoot[c_tree_shoot.Count - 1].gameObject); // c_tree_shoot[c_tree_shoot.Count - 1].gameObject.BringToFront(); // lCtree.Add(c_tree_shoot[c_tree_shoot.Count - 1]); // break; } }
public void Insert(CTreeObject _object) { if (Node[0] != null) { AddObjectInNode(_object); return; } objects.Add(_object); if (!canSplit()) return; if (Node[0] == null) Split(); int i = 0; while(i<objects.Count) { AddObjectInNode(objects[i]); objects.RemoveAt(i); } }