Exemple #1
0
func NewImageElement(img image.Image) *ImageElement {
	imageElement := &ImageElement{
		rotation: 0,
		Hitbox:   NewHitbox(),
		node:     renderer.NewNode(),
	}
	box := renderer.CreateBoxWithOffset(1, 1, 0, 0)
	imageElement.node.Add(box)
	imageElement.SetImage(img)
	return imageElement
}
Exemple #2
0
func NewWindow() *Window {
	node := renderer.NewNode()
	elementNode := renderer.NewNode()
	background := renderer.NewNode()
	background.Material = renderer.NewMaterial()
	box := renderer.CreateBoxWithOffset(1, 1, 0, 0)
	box.SetColor(color.NRGBA{255, 255, 255, 255})
	background.Add(box)
	node.Add(background)
	node.Add(elementNode)
	return &Window{
		node:          node,
		backgroundBox: box,
		background:    background,
		elementNode:   elementNode,
		size:          mgl32.Vec2{500, 1},
		Tabs:          []Activatable{},
	}
}
Exemple #3
0
func NewContainer() *Container {
	node := renderer.NewNode()
	elementsNode := renderer.NewNode()
	background := renderer.NewNode()
	box := renderer.CreateBoxWithOffset(1, 1, 0, 0)
	box.SetColor(color.NRGBA{0, 0, 0, 0})
	background.Material = renderer.NewMaterial()
	background.Add(box)
	node.Add(background)
	node.Add(elementsNode)
	return &Container{
		node:          node,
		elementsNode:  elementsNode,
		background:    background,
		backgroundBox: box,
		children:      make([]Element, 0),
		Hitbox:        NewHitbox(),
		padding:       NewMargin(0),
		margin:        NewMargin(0),
	}
}
Exemple #4
0
func NewTextField(text string, textColor color.Color, textSize float32, textFont *truetype.Font) *TextField {
	cursor := renderer.CreateBoxWithOffset(0.07, 1.1, 0, 0.1)
	cursor.SetColor(color.NRGBA{0, 0, 0, 255})
	cursorNode := renderer.NewNode()
	cursorNode.Material = renderer.NewMaterial()
	cursorNode.Add(cursor)
	tf := &TextField{
		container: NewContainer(),
		text:      NewTextElement(text, textColor, textSize, textFont),
		cursor:    cursorNode,
		cursorPos: len(text),
	}
	tf.text.node.Add(cursorNode)
	tf.container.AddChildren(tf.text)
	tf.GetHitbox().AddOnClick(func(button int, release bool, position mgl32.Vec2) {
		if !release {
			tf.Activate()
		}
	})
	tf.SetBackgroundColor(0, 0, 0, 0)
	tf.SetHeight(textSize * 1.5)
	tf.SetPadding(NewMargin(2))
	return tf
}
Exemple #5
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()
		})
	})
}