Beispiel #1
0
func ImportShader(vertexFile, fragmentFile string) (*renderer.Shader, error) {
	vertsrc, err := ioutil.ReadFile(vertexFile)
	if err != nil {
		fmt.Printf("Error vertex file: %v\n", err)
		return nil, err
	}

	fragsrc, err := ioutil.ReadFile(fragmentFile)
	if err != nil {
		fmt.Printf("Error fragment file: %v\n", err)
		return nil, err
	}

	shader := renderer.NewShader()
	shader.VertSrc = string(vertsrc)
	shader.FragSrc = string(fragsrc)
	return shader, nil
}
Beispiel #2
0
func main() {

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

	gameEngine.Start(func() {
		// load in ui shader
		shader := renderer.NewShader()
		shader.FragSrc = uiFragmentShader
		shader.VertSrc = uiVertexShader
		gameEngine.DefaultShader(shader)

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

		// create windows with content containers
		window1, content1 := createWindow(controllerManager)
		window2, content2 := createWindow(controllerManager)

		// populate content and set window tab order
		window1.Tabs = populateContent(content1)
		window2.Tabs = htmlContent(content2)

		// position windows
		window1.SetTranslation(mgl32.Vec2{50, 50}.Vec3(0))
		window2.SetTranslation(mgl32.Vec2{450, 50}.Vec3(0))

		// Add the windows to the engine
		gameEngine.AddOrtho(window1)
		gameEngine.AddOrtho(window2)

		// render all windows
		window1.Render()
		window2.Render()
	})
}
Beispiel #3
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()
		})
	})
}