Пример #1
0
// createBodies sets up the chipmunk space and static bodies
func createBodies() {
	space = chipmunk.NewSpace()
	space.Gravity = vect.Vect{0, -900}

	staticBody := chipmunk.NewBodyStatic()
	staticLines = []*chipmunk.Shape{
		chipmunk.NewSegment(vect.Vect{111.0, 280.0}, vect.Vect{407.0, 246.0}, 0),
		chipmunk.NewSegment(vect.Vect{407.0, 246.0}, vect.Vect{407.0, 343.0}, 0),
	}
	for _, segment := range staticLines {
		segment.SetElasticity(0.6)
		staticBody.AddShape(segment)
	}
	space.AddBody(staticBody)
}
Пример #2
0
func newGround(x1, y1, x2, y2 float32) *ground {
	ground := new(ground)

	// Chipmunk body

	ground.physicsBody = chipmunk.NewBodyStatic()
	ground.physicsShape = chipmunk.NewSegment(
		vect.Vect{vect.Float(x1), vect.Float(y1)},
		vect.Vect{vect.Float(x2), vect.Float(y2)},
		GroundRadius,
	)

	ground.physicsBody.AddShape(ground.physicsShape)

	// OpenGL shape

	ground.openglShape = shapes.NewSegment(x1, y1, x2, y2)
	ground.openglShape.Color(color.White)

	return ground
}
Пример #3
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))

}
Пример #4
0
func main() {

	//renderer and game engine
	glRenderer := opengl.NewOpenglRenderer("Platformer", 800, 800, false)
	gameEngine := engine.NewEngine(glRenderer)
	gameEngine.InitFpsDial()

	// physics engine (Chipmonk)
	physicsSpace := chipmunkPhysics.NewChipmonkSpace()
	physicsSpace.SetGravity(mgl32.Vec2{0, 400})
	gameEngine.AddUpdatable(physicsSpace)

	gameEngine.Start(func() {
		glRenderer.BackGroundColor(0.7, 0.7, 0.9, 0.0)

		// load in default shader
		shader := renderer.NewShader()
		shader.FragSrc = fragShader
		shader.VertSrc = vertShader
		gameEngine.DefaultShader(shader)

		// The player object
		character := NewCharacter()
		character.body.SetPosition(mgl32.Vec2{400, 400})

		// Add the character to all the things
		physicsSpace.AddBody(character.body)
		gameEngine.AddOrtho(character.sprite)
		gameEngine.AddUpdatable(character)

		// terrain
		terrainGeometry := renderer.CreateBoxWithOffset(800, 800-floorHeight, 0, floorHeight)
		terrainGeometry.SetColor(color.NRGBA{0, 80, 0, 254})
		gameEngine.AddOrtho(terrainGeometry)

		// terrain physics body
		terrainBody := chipmunkPhysics.NewChipmunkBodyStatic()
		segment := chipmunk.NewSegment(vect.Vect{0, floorHeight}, vect.Vect{4000, floorHeight}, 0)
		terrainBody.Body.AddShape(segment)
		physicsSpace.AddBody(terrainBody)

		particleNode := renderer.NewNode()
		gameEngine.AddOrtho(particleNode)

		// create a new timed particle system
		spawnParticles := func(load func() *effects.ParticleGroup, position mgl32.Vec2) {
			particleGroup := load()
			particleGroup.SetTranslation(position.Vec3(0))
			effects.TriggerTimedParticleGroup(
				effects.TimedParticleGroup{
					Particles:  particleGroup,
					GameEngine: gameEngine,
					TargetNode: particleNode,
					Life:       0.2,
					Cleanup:    10,
				},
			)
		}

		// input/controller manager
		controllerManager := glfwController.NewControllerManager(glRenderer.Window)

		// key bindings
		customController := controller.CreateController()
		controllerManager.AddController(customController.(glfwController.Controller))

		// Walk
		customController.BindKeyAction(func() {
			character.body.SetVelocity(mgl32.Vec2{-200, character.body.GetVelocity().Y()})
		}, controller.KeyA, controller.Press)
		customController.BindKeyAction(func() {
			character.body.SetVelocity(mgl32.Vec2{200, character.body.GetVelocity().Y()})
		}, controller.KeyD, controller.Press)

		//Stop walking
		customController.BindKeyAction(func() {
			character.body.SetVelocity(mgl32.Vec2{0, character.body.GetVelocity().Y()})
		}, controller.KeyA, controller.Release)
		customController.BindKeyAction(func() {
			character.body.SetVelocity(mgl32.Vec2{0, character.body.GetVelocity().Y()})
		}, controller.KeyD, controller.Release)

		// Jump
		customController.BindKeyAction(func() {
			character.body.SetVelocity(character.body.GetVelocity().Add(mgl32.Vec2{0, -300}))
			spawnParticles(dustParticles, character.body.GetPosition().Add(mgl32.Vec2{0, 0.5 * characterSize}))
		}, controller.KeySpace, controller.Press)

		// shoot
		customController.BindMouseAction(func() {
			spawnParticles(majicParticles, character.body.GetPosition())
		}, controller.MouseButtonLeft, controller.Press)

		// create sparks when player collides with somthing
		var collisionTimer time.Time
		physicsSpace.SetOnCollision(func(shapeA, shapeB *chipmunk.Shape) {
			if time.Since(collisionTimer) > 200*time.Millisecond {
				spawnParticles(sparkParticles, character.body.GetPosition().Add(mgl32.Vec2{0, 0.5 * characterSize}))
			}
			collisionTimer = time.Now()
		})
	})
}
Пример #5
0
func (self LevelPoint) getChipmunkSegment() *chipmunk.Shape {
	return chipmunk.NewSegment(vect.Vect{vect.Float(self.Ax), vect.Float(self.Ay)}, vect.Vect{vect.Float(self.Bx), vect.Float(self.By)}, 0)
}