Пример #1
0
func (m *MoveFlag) checkCollisionWithWalls(obj gameObjectsBase.Activer, dir int, shift float64) (bool, geometry.Point) {
	pos := obj.GetShiftedFrontSide(dir, shift)
	if m.field.IsBlocked(int(math.Floor(pos.X)), int(math.Floor(pos.Y))) {
		switch dir {
		case consts.NORTH_DIR:
			pos.Y = math.Ceil(pos.Y) + consts.OBJECT_HALF
		case consts.SOUTH_DIR:
			pos.Y = math.Floor(pos.Y) - consts.OBJECT_HALF
		case consts.EAST_DIR:
			pos.X = math.Floor(pos.X) - consts.OBJECT_HALF
		case consts.WEST_DIR:
			pos.X = math.Ceil(pos.X) + consts.OBJECT_HALF
		}
		return false, pos
	}
	eps := consts.SLIDE_THRESHOLD
	side, pos := obj.GetCollisionableSide(dir, shift)
	res1 := m.field.IsBlocked(int(side.Point1.X), int(side.Point1.Y))
	res2 := m.field.IsBlocked(int(side.Point2.X), int(side.Point2.Y))
	var near float64
	if res1 || res2 {
		switch dir {
		case consts.NORTH_DIR, consts.SOUTH_DIR:
			if res1 {
				near = math.Ceil(side.Point1.X) - side.Point1.X
			} else {
				near = math.Floor(side.Point1.X) - side.Point1.X
			}
			if math.Abs(near) <= eps {
				side.Point1.X = side.Point1.X + near
				side.Point2.X = side.Point2.X + near
			} else {
				return false, obj.GetCenter()
			}
			pos.X = (side.Point1.X + side.Point2.X) / 2
		case consts.EAST_DIR, consts.WEST_DIR:
			if res1 {
				near = math.Ceil(side.Point1.Y) - side.Point1.Y
			} else {
				near = math.Floor(side.Point1.Y) - side.Point1.Y
			}
			if math.Abs(near) <= eps {
				side.Point1.Y = side.Point1.Y + near
				side.Point2.Y = side.Point2.Y + near
			} else {
				return false, obj.GetCenter()
			}
			pos.Y = (side.Point1.Y + side.Point2.Y) / 2
		}
	}
	return true, pos
}