예제 #1
0
func (m *Map) GenerateCollision() {

	var shapes []*chipmunk.Shape

	for x := 0; x < m.Width; x++ {
		for y := 0; y < m.Height; y++ {
			if t, e := m.GetTile(x, y); e {
				if t.Collision() != CollisionNone {
					p, _ := m.GetTilePos(x, y)
					shape := chipmunk.NewBox(vect.Vect{vect.Float(p.X), vect.Float(p.Y)},
						vect.Float(m.TileSize), vect.Float(m.TileSize))

					shape.Layer = chipmunk.Layer(m.Layer)
					shape.SetFriction(0)
					shapes = append(shapes, shape)
				}
			}
		}
	}

	m.GameObject().AddComponent(engine.NewPhysicsShapes(true, shapes))

}
예제 #2
0
func (m *Map) GenerateCollision2() {
	tilesy := make(map[int][]int)
	tilesx := make(map[int][]int)
	for x := 0; x < m.Width; x++ {
		for y := 0; y < m.Height; y++ {
			if t, e := m.GetTile(x, y); e {
				if t.Collision() != CollisionNone {
					tilesy[y] = append(tilesy[y], x)
					tilesx[x] = append(tilesx[x], y)
				}
			}
		}
	}

	var shapes []*chipmunk.Shape

	centerx := vect.Float(m.TileSize * float32(m.Width) / 2)
	centery := vect.Float(m.TileSize * float32(m.Height) / 2)

	println("Map layer", m.Layer)
	for y, xarr := range tilesy {
		minx := xarr[0]
		maxx := xarr[0]
		sort.Ints(xarr)

		for i := 1; i < len(xarr); i++ {
			x := xarr[i]
			if maxx+1 == x {
				maxx = x
			} else {
				if maxx-minx > 1 {
					shapes = append(shapes, chipmunk.NewSegment(
						vect.Vect{vect.Float(float32(minx)*m.TileSize) - centerx, -vect.Float(float32(y)*m.TileSize) + centery},
						vect.Vect{vect.Float(float32(maxx)*m.TileSize) - centerx, -vect.Float(float32(y)*m.TileSize) + centery},
						1))
					println(y, minx, maxx)
				}
				minx = x
				maxx = x
			}
		}
		if maxx-minx > 1 {
			shapes = append(shapes, chipmunk.NewSegment(
				vect.Vect{vect.Float(float32(minx)*m.TileSize) - centerx, -vect.Float(float32(y)*m.TileSize) + centery},
				vect.Vect{vect.Float(float32(maxx)*m.TileSize) - centerx, -vect.Float(float32(y)*m.TileSize) + centery},
				1))
			println(y, minx, maxx)
		}

	}

	for x, yarr := range tilesx {
		miny := yarr[0]
		maxy := yarr[0]
		sort.Ints(yarr)
		println(len(yarr))
		for i := 1; i < len(yarr); i++ {
			y := yarr[i]
			if maxy+1 == y {
				maxy = y
			} else {
				if maxy-miny > 1 {
					shapes = append(shapes, chipmunk.NewSegment(
						vect.Vect{vect.Float(float32(x)*m.TileSize) - centerx, -vect.Float(float32(miny)*m.TileSize) + centery},
						vect.Vect{vect.Float(float32(x)*m.TileSize) - centerx, -vect.Float(float32(maxy)*m.TileSize) + centery},
						1))
					println(x, miny, maxy)
				}
				miny = y
				maxy = y
			}
		}
		if maxy-miny > 1 {
			shapes = append(shapes, chipmunk.NewSegment(
				vect.Vect{vect.Float(float32(x)*m.TileSize) - centerx, -vect.Float(float32(miny)*m.TileSize) + centery},
				vect.Vect{vect.Float(float32(x)*m.TileSize) - centerx, -vect.Float(float32(maxy)*m.TileSize) + centery},
				1))
			println(x, miny, maxy)
		}

	}
	println("Map end")
	m.GameObject().AddComponent(engine.NewPhysicsShapes(true, shapes))

}