예제 #1
0
파일: main.go 프로젝트: walesey/go-engine
func sparkParticles() *effects.ParticleSystem {
	img, _ := assets.ImportImageCached("TestAssets/Spark.png")
	smoke := effects.CreateParticleSystem(effects.ParticleSettings{
		MaxParticles:     7,
		ParticleEmitRate: 7,
		BaseGeometry:     renderer.CreateBox(float32(1), float32(1)),
		TotalFrames:      1,
		FramesX:          1,
		FramesY:          1,
		MaxLife:          1.0,
		MinLife:          0.7,
		StartColor:       color.NRGBA{255, 220, 180, 255},
		EndColor:         color.NRGBA{255, 155, 55, 255},
		StartSize:        mgl32.Vec3{1, 1, 1}.Mul(0.008),
		EndSize:          mgl32.Vec3{1, 1, 1}.Mul(0.005),
		MinTranslation:   mgl32.Vec3{1, 1, 1}.Mul(-0.04),
		MaxTranslation:   mgl32.Vec3{1, 1, 1}.Mul(0.04),
		MinStartVelocity: mgl32.Vec3{-0.5, 0.0, -0.5},
		MaxStartVelocity: mgl32.Vec3{0.5, 1.0, 0.5},
		Acceleration:     mgl32.Vec3{0.0, -1.0, 0.0},
		OnParticleUpdate: func(p *effects.Particle) {
			p.Scale[0] = p.Scale[0] * (1 + p.Velocity.Len()*3.5)
			p.Orientation = mgl32.QuatBetweenVectors(mgl32.Vec3{1, 0, 0}, p.Velocity)
		},
	})
	smoke.Node.Material = renderer.NewMaterial(renderer.NewTexture("diffuseMap", img, false))
	return smoke
}
예제 #2
0
파일: main.go 프로젝트: walesey/go-engine
func fireParticles() *effects.ParticleSystem {
	img, _ := assets.ImportImageCached("TestAssets/Fire.png")
	fire := effects.CreateParticleSystem(effects.ParticleSettings{
		MaxParticles:     12,
		ParticleEmitRate: 3,
		BaseGeometry:     renderer.CreateBox(float32(1), float32(1)),
		TotalFrames:      36,
		FramesX:          6,
		FramesY:          6,
		MaxLife:          0.8,
		MinLife:          1.5,
		StartColor:       color.NRGBA{255, 180, 80, 255},
		EndColor:         color.NRGBA{255, 60, 20, 255},
		StartSize:        mgl32.Vec3{1, 1, 1}.Mul(0.16),
		EndSize:          mgl32.Vec3{1, 1, 1}.Mul(0.23),
		MinTranslation:   mgl32.Vec3{1, 1, 1}.Mul(-0.01),
		MaxTranslation:   mgl32.Vec3{1, 1, 1}.Mul(0.01),
		MinStartVelocity: mgl32.Vec3{-0.02, 0, -0.02},
		MaxStartVelocity: mgl32.Vec3{0.02, 0.08, 0.02},
		MinRotation:      3.0,
		MaxRotation:      3.6,
	})
	fire.Node.Material = renderer.NewMaterial(renderer.NewTexture("diffuseMap", img, false))
	return fire
}
예제 #3
0
파일: main.go 프로젝트: walesey/go-engine
func sparkParticles() *effects.ParticleGroup {
	img, _ := assets.ImportImageCached("resources/spark.png")
	material := renderer.NewMaterial(renderer.NewTexture("diffuseMap", img, false))
	particleSystem := effects.CreateParticleSystem(effects.ParticleSettings{
		MaxParticles:     80,
		ParticleEmitRate: 400,
		BaseGeometry:     renderer.CreateBox(float32(1), float32(1)),
		TotalFrames:      1,
		FramesX:          1,
		FramesY:          1,
		MaxLife:          3.3,
		MinLife:          1.7,
		StartColor:       color.NRGBA{254, 160, 90, 254},
		EndColor:         color.NRGBA{254, 160, 90, 254},
		StartSize:        mgl32.Vec2{2, 2}.Vec3(0),
		EndSize:          mgl32.Vec2{2, 2}.Vec3(0),
		MinTranslation:   mgl32.Vec2{-5, -5}.Vec3(0),
		MaxTranslation:   mgl32.Vec2{5, 5}.Vec3(0),
		MinStartVelocity: mgl32.Vec2{-100, -100}.Vec3(0),
		MaxStartVelocity: mgl32.Vec2{100, 100}.Vec3(0),
		Acceleration:     mgl32.Vec2{0, 400}.Vec3(0),
		OnParticleUpdate: func(p *effects.Particle) {
			p.Scale[0] = p.Scale[0] * (1 + p.Velocity.Len()*0.05)
			p.Orientation = mgl32.QuatBetweenVectors(mgl32.Vec3{1, 0, 0}, p.Velocity)
		},
	})
	particleSystem.FaceCamera = false
	particleGroup := effects.NewParticleGroup(nil, particleSystem)
	particleGroup.Node.Material = material
	return particleGroup
}
예제 #4
0
파일: main.go 프로젝트: walesey/go-engine
func majicParticles() *effects.ParticleGroup {
	img, _ := assets.ImportImageCached("resources/majic.png")
	material := renderer.NewMaterial(renderer.NewTexture("diffuseMap", img, false))
	particleSystem := effects.CreateParticleSystem(effects.ParticleSettings{
		MaxParticles:     300,
		ParticleEmitRate: 700,
		BaseGeometry:     renderer.CreateBox(float32(1), float32(1)),
		TotalFrames:      9,
		FramesX:          3,
		FramesY:          3,
		MaxLife:          1.3,
		MinLife:          0.7,
		StartColor:       color.NRGBA{254, 254, 254, 254},
		EndColor:         color.NRGBA{254, 254, 254, 254},
		StartSize:        mgl32.Vec2{10, 10}.Vec3(0),
		EndSize:          mgl32.Vec2{10, 10}.Vec3(0),
		MinTranslation:   mgl32.Vec2{-1, -1}.Vec3(0),
		MaxTranslation:   mgl32.Vec2{1, 1}.Vec3(0),
		MinStartVelocity: mgl32.Vec2{-170, -170}.Vec3(0),
		MaxStartVelocity: mgl32.Vec2{170, 170}.Vec3(0),
		MaxRotation:      -3.14,
		MinRotation:      3.14,
	})
	particleSystem.FaceCamera = false
	particleGroup := effects.NewParticleGroup(nil, particleSystem)
	particleGroup.Node.Material = material
	return particleGroup
}
예제 #5
0
파일: main.go 프로젝트: walesey/go-engine
func dustParticles() *effects.ParticleGroup {
	img, _ := assets.ImportImageCached("resources/smoke.png")
	material := renderer.NewMaterial(renderer.NewTexture("diffuseMap", img, false))
	particleSystem := effects.CreateParticleSystem(effects.ParticleSettings{
		MaxParticles:     5,
		ParticleEmitRate: 20,
		BaseGeometry:     renderer.CreateBox(float32(1), float32(1)),
		TotalFrames:      64,
		FramesX:          8,
		FramesY:          8,
		MaxLife:          3.3,
		MinLife:          2.7,
		StartColor:       color.NRGBA{254, 254, 254, 120},
		EndColor:         color.NRGBA{254, 254, 254, 0},
		StartSize:        mgl32.Vec2{40, 40}.Vec3(0),
		EndSize:          mgl32.Vec2{180, 180}.Vec3(0),
		MinTranslation:   mgl32.Vec2{-2, -2}.Vec3(0),
		MaxTranslation:   mgl32.Vec2{2, 2}.Vec3(0),
		MinStartVelocity: mgl32.Vec2{-5.0, -5.0}.Vec3(0),
		MaxStartVelocity: mgl32.Vec2{5.0, 5.0}.Vec3(0),
		MaxRotation:      -3.14,
		MinRotation:      3.14,
	})
	particleSystem.FaceCamera = false
	particleGroup := effects.NewParticleGroup(nil, particleSystem)
	particleGroup.Node.Material = material
	return particleGroup
}
예제 #6
0
파일: sprite.go 프로젝트: walesey/go-engine
func CreateSprite(totalFrames, framesX, framesY int, material *renderer.Material) *Sprite {
	sprite := Sprite{
		frame:       0,
		FaceCamera:  true,
		totalFrames: totalFrames,
		framesX:     framesX,
		framesY:     framesY,
	}
	geometry := renderer.CreateBox(1, 1)
	sprite.geometry = geometry
	sprite.Node = renderer.NewNode()
	sprite.Node.Material = material
	sprite.Node.Add(sprite.geometry)
	return &sprite
}
예제 #7
0
파일: main.go 프로젝트: walesey/go-engine
func main() {
	server := len(os.Args) > 1 && os.Args[1] == "server"
	var gameEngine engine.Engine
	var glRenderer *opengl.OpenglRenderer
	network := networking.NewNetwork()

	// Start server or connect to server
	if server {
		gameEngine = engine.NewHeadlessEngine()
		network.StartServer(serverPort)
	} else {
		glRenderer = opengl.NewOpenglRenderer("Networking example", 800, 800, false)
		gameEngine = engine.NewEngine(glRenderer)
		network.ConnectClient(fmt.Sprintf("%v:%v", serverAddr, serverPort))
	}
	gameEngine.AddUpdatable(network)

	// map containing each player's entity
	players := make(map[string]*Player)

	//Networked Game events
	network.ClientJoinedEvent(func(clientId string) {
		fmt.Println("client joined, clientId: ", clientId)
		network.TriggerOnServerAndClients("spawn", util.SerializeArgs(clientId, startingPosition))
		for _, player := range players {
			network.TriggerEvent("spawn", clientId, util.SerializeArgs(player.clientId, player.position))
		}
	})

	network.RegisterEvent("spawn", func(clientId string, data []byte) {
		buf := bytes.NewBuffer(data)
		playerID := util.Stringfrombytes(buf)
		position := util.Vector2frombytes(buf)
		if _, ok := players[playerID]; !ok {
			player := &Player{clientId: playerID}
			players[player.clientId] = player
			player.node = renderer.NewNode()
			player.position = position
			gameEngine.AddUpdatable(player)
			if network.IsClient() {
				boxGeometry := renderer.CreateBox(30, 30)
				boxGeometry.SetColor(color.NRGBA{254, 0, 0, 254})
				player.node.Add(boxGeometry)
				gameEngine.AddOrtho(player.node)
			}
		}
	})

	network.RegisterEvent("move", func(clientId string, data []byte) {
		buf := bytes.NewBuffer(data)
		playerID := util.Stringfrombytes(buf)
		velocity := util.Vector2frombytes(buf)
		if network.IsServer() && clientId != playerID {
			return // client is only allowed to control the player assigned to them.
		}
		if player, ok := players[playerID]; ok {
			player.velocity = velocity
			if network.IsServer() {
				network.BroadcastEvent("updatePlayer", util.SerializeArgs(playerID, player.position, player.velocity))
				network.FlushAllWriteBuffers()
			}
		}
	})

	network.RegisterEvent("updatePlayer", func(clientId string, data []byte) {
		if network.IsClient() { // This is a server to client update only
			buf := bytes.NewBuffer(data)
			playerID := util.Stringfrombytes(buf)
			position := util.Vector2frombytes(buf)
			velocity := util.Vector2frombytes(buf)
			if player, ok := players[playerID]; ok {
				player.position = position
				player.velocity = velocity
			}
		}
	})

	// client setup
	gameEngine.Start(func() {
		if network.IsClient() {
			gameEngine.InitFpsDial()

			if shader, err := assets.ImportShader("shaders/build/basic.vert", "shaders/build/basic.frag"); err == nil {
				gameEngine.DefaultShader(shader)
			}

			glRenderer.BackGroundColor(0, 0.4, 0, 0)

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

			// networked movement controls
			move := func(velocity mgl32.Vec2) {
				network.TriggerOnServerAndClients("move", util.SerializeArgs(network.ClientToken(), velocity))
			}

			customController := controller.CreateController()
			controllerManager.AddController(customController.(glfwController.Controller))
			customController.BindKeyAction(func() { move(mgl32.Vec2{0, -100}) }, controller.KeyW, controller.Press)
			customController.BindKeyAction(func() { move(mgl32.Vec2{-100, 0}) }, controller.KeyA, controller.Press)
			customController.BindKeyAction(func() { move(mgl32.Vec2{0, 100}) }, controller.KeyS, controller.Press)
			customController.BindKeyAction(func() { move(mgl32.Vec2{100, 0}) }, controller.KeyD, controller.Press)
			customController.BindKeyAction(func() { move(mgl32.Vec2{}) }, controller.KeyW, controller.Release)
			customController.BindKeyAction(func() { move(mgl32.Vec2{}) }, controller.KeyA, controller.Release)
			customController.BindKeyAction(func() { move(mgl32.Vec2{}) }, controller.KeyS, controller.Release)
			customController.BindKeyAction(func() { move(mgl32.Vec2{}) }, controller.KeyD, controller.Release)
		}
	})
}
예제 #8
0
파일: main.go 프로젝트: walesey/go-engine
func main() {

	glRenderer := opengl.NewOpenglRenderer("Simple", 800, 800, false)
	gameEngine := engine.NewEngine(glRenderer)
	gameEngine.InitFpsDial()

	gameEngine.Start(func() {

		if shader, err := assets.ImportShader("shaders/build/basic.vert", "shaders/build/basic.frag"); err == nil {
			gameEngine.DefaultShader(shader)
		}

		// sky cube
		skyImg, err := assets.ImportImage("resources/cubemap.png")
		if err == nil {
			geom := renderer.CreateSkyBox()
			geom.Transform(mgl32.Scale3D(10000, 10000, 10000))
			skyNode := renderer.NewNode()
			skyNode.SetOrientation(mgl32.QuatRotate(1.57, mgl32.Vec3{0, 1, 0}))
			skyNode.Material = renderer.NewMaterial(renderer.NewTexture("diffuseMap", skyImg, false))
			skyNode.RendererParams = renderer.NewRendererParams()
			skyNode.RendererParams.CullBackface = false
			skyNode.RendererParams.Unlit = true
			skyNode.Add(geom)
			gameEngine.AddSpatial(skyNode)
		}

		// Add some light to the scene
		ambientLight := renderer.NewLight(renderer.AMBIENT)
		ambientLight.Color = [3]float32{0.3, 0.3, 0.3}
		gameEngine.AddLight(ambientLight)

		// Create a red box geometry, attach to a node, add the node to the scenegraph
		boxGeometry := renderer.CreateBox(10, 10)
		boxGeometry.SetColor(color.NRGBA{254, 0, 0, 254})
		boxNode := renderer.NewNode()
		boxNode.RendererParams = renderer.NewRendererParams()
		boxNode.RendererParams.CullBackface = false
		boxNode.Material = renderer.NewMaterial()
		boxNode.SetTranslation(mgl32.Vec3{30, 0})
		boxNode.Add(boxGeometry)
		gameEngine.AddSpatial(boxNode)

		// make the box spin
		var angle float64
		gameEngine.AddUpdatable(engine.UpdatableFunc(func(dt float64) {
			angle += dt
			q := mgl32.QuatRotate(float32(angle), mgl32.Vec3{0, 1, 0})
			boxNode.SetOrientation(q)
		}))

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

		// camera + wasd controls
		camera := gameEngine.Camera()
		freeMoveActor := actor.NewFreeMoveActor(camera)
		freeMoveActor.Location = mgl32.Vec3{}
		mainController := controller.NewBasicMovementController(freeMoveActor, false)
		controllerManager.AddController(mainController.(glfwController.Controller))
		gameEngine.AddUpdatable(freeMoveActor)

		//lock the cursor
		glRenderer.LockCursor(true)

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

		// close window and exit on escape
		customController.BindKeyAction(func() {
			glRenderer.Window.SetShouldClose(true)
		}, controller.KeyEscape, controller.Press)
	})
}