Пример #1
0
func (s *GameScene) Load() {
	Players = make(map[server.ID]*engine.GameObject)
	LoadTextures()
	engine.SetTitle("Space Cookies")
	queenDead = false

	var e error
	fireSound, e = wav.NewClip("./data/laser5.wav")
	if e != nil {
		fmt.Println(e)
	}

	rand.Seed(time.Now().UnixNano())

	GameSceneGeneral = s

	s.Camera = engine.NewCamera()

	cam := engine.NewGameObject("Camera")
	cam.AddComponent(s.Camera)

	cam.Transform().SetScalef(1, 1)

	gui := engine.NewGameObject("GUI")

	Layer1 := engine.NewGameObject("Layer1")
	Layer2 := engine.NewGameObject("Layer2")
	Layer3 := engine.NewGameObject("Layer3")
	Layer4 := engine.NewGameObject("Layer3")

	s.Layer1 = Layer1
	s.Layer2 = Layer2
	s.Layer3 = Layer3
	s.Layer4 = Layer4

	mouse := engine.NewGameObject("Mouse")
	mouse.AddComponent(engine.NewMouse())
	mouse.AddComponent(NewMouseDebugger())
	mouse.Transform().SetParent2(cam)

	FPSDrawer := engine.NewGameObject("FPS")
	FPSDrawer.Transform().SetParent2(cam)
	txt := FPSDrawer.AddComponent(components.NewUIText(ArialFont2, "")).(*components.UIText)
	fps := FPSDrawer.AddComponent(engine.NewFPS()).(*engine.FPS)
	fps.SetAction(func(fps float64) {
		txt.SetString("FPS: " + strconv.FormatFloat(fps, 'f', 2, 32))
	})
	txt.SetAlign(engine.AlignLeft)

	FPSDrawer.Transform().SetPositionf(-float32(engine.Width)/2+20, +float32(engine.Height)/2-20)
	FPSDrawer.Transform().SetScalef(20, 20)
	/*
		label := engine.NewGameObject("Label")
		label.Transform().SetParent2(cam)
		label.Transform().SetPositionf(20, float32(engine.Height)-40)
		label.Transform().SetScalef(20, 20)

		txt2 := label.AddComponent(components.NewUIText(ArialFont2, "Input: ")).(*components.UIText)
		txt2.SetFocus(true)
		txt2.SetWritable(true)
		txt2.SetAlign(engine.AlignLeft)
	*/
	//SPACCCEEEEE
	engine.Space.Gravity.Y = 0
	engine.Space.Iterations = 10

	uvs, ind := engine.AnimatedGroupUVs(atlas, Explosion_ID)
	Explosion = engine.NewGameObject("Explosion")
	Explosion.AddComponent(engine.NewSprite3(atlas.Texture, uvs))
	Explosion.Sprite.BindAnimations(ind)
	Explosion.Sprite.AnimationSpeed = 25
	Explosion.Sprite.AnimationEndCallback = func(sprite *engine.Sprite) {
		sprite.GameObject().Destroy()
	}
	Explosion.Transform().SetScalef(30, 30)
	Explosion.Transform().SetDepth(1)

	missleGameObject := engine.NewGameObject("Missle")
	missleGameObject.AddComponent(engine.NewSprite2(atlas.Texture, engine.IndexUV(atlas, Missle_A)))
	missleGameObject.AddComponent(engine.NewPhysics(false))
	missleGameObject.Transform().SetScalef(20, 20)
	missleGameObject.AddComponent(NewDamageDealer(50))
	missleGameObject.Physics.Shape.IsSensor = true
	missle = NewMissle(30000)
	missleGameObject.AddComponent(missle)
	missle.Explosion = Explosion
	ds := NewDestoyable(0, 1)
	ds.SetDestroyTime(1)
	missleGameObject.AddComponent(ds)

	ship := engine.NewGameObject("Ship")
	Player = ship
	Player.Transform().SetParent2(Layer2)
	Player.AddComponent(MyClient)
	/*
		Health := engine.NewGameObject("HP")
		Health.Transform().SetParent2(cam)
		Health.Transform().SetPositionf(150, 50)

		HealthGUI := engine.NewGameObject("HPGUI")
		HealthGUI.AddComponent(engine.NewSprite2(atlas.Texture, engine.IndexUV(atlas, HPGUI_A)))
		HealthGUI.Transform().SetParent2(Health)
		HealthGUI.Transform().SetScalef(50, 50)

		HealthBar := engine.NewGameObject("HealthBar")
		HealthBar.Transform().SetParent2(Health)
		HealthBar.Transform().SetPositionf(-82, 0)
		HealthBar.Transform().SetScalef(100, 50)

		uvHP := engine.IndexUV(atlas, HP_A)

		HealthBarGUI := engine.NewGameObject("HealthBarGUI")
		HealthBarGUI.Transform().SetParent2(HealthBar)
		HealthBarGUI.AddComponent(engine.NewSprite2(atlas.Texture, uvHP))
		HealthBarGUI.Transform().SetScalef(0.52, 1)
		HealthBarGUI.Transform().SetPositionf((uvHP.Ratio/2)*HealthBarGUI.Transform().Scale().X, 0)

		JetFire := engine.NewGameObject("Jet")
		JetFire.AddComponent(engine.NewSprite2(atlas.Texture, engine.IndexUV(atlas, Jet_A)))

		ship.AddComponent(engine.NewSprite2(atlas.Texture, engine.IndexUV(atlas, SpaceShip_A)))
		PlayerShip = ship.AddComponent(NewShipController()).(*ShipController)
		ship.Transform().SetParent2(Layer2)
		ship.Transform().SetPositionf(400, 200)
		ship.Transform().SetScalef(100, 100)
		shipHP := float32(1000)
		PlayerShip.HPBar = HealthBar
		PlayerShip.JetFire = JetFire
		PlayerShip.Missle = missle
		ship.AddComponent(NewDestoyable(shipHP, 1))
	*/

	cookie = engine.NewGameObject("Cookie")
	cookie.AddComponent(engine.NewSprite(cir))
	cookie.AddComponent(NewDestoyable(100, 2))
	cookie.AddComponent(NewDamageDealer(20))
	cookie.AddComponent(NewEnemeyAI(Player, Enemey_Cookie))
	cookie.Transform().SetScalef(50, 50)
	cookie.Transform().SetPositionf(400, 400)
	cookie.AddComponent(engine.NewPhysicsShape(false, chipmunk.NewCircle(vect.Vect{0, 0}, 25)))
	cookie.Tag = CookieTag

	defender = engine.NewGameObject("Box")
	ds = NewDestoyable(30, 3)
	ds.SetDestroyTime(5)
	defender.AddComponent(ds)
	defender.AddComponent(engine.NewSprite(boxt))
	defender.Tag = CookieTag
	defender.Transform().SetScalef(50, 50)

	phx := defender.AddComponent(engine.NewPhysics(false)).(*engine.Physics)
	phx.Body.SetMass(2.5)
	phx.Body.SetMoment(phx.Shape.Moment(2.5))
	phx.Shape.SetFriction(0.5)
	//phx.Shape.Group = 2
	phx.Shape.SetElasticity(0.5)

	QueenCookie := engine.NewGameObject("Cookie")
	QueenCookie.AddComponent(engine.NewSprite2(atlas.Texture, engine.IndexUV(atlas, Queen_A)))
	QueenCookie.AddComponent(NewDestoyable(5000, 2))
	QueenCookie.AddComponent(NewDamageDealer(200))
	QueenCookie.AddComponent(NewEnemeyAI(Player, Enemey_Boss))
	QueenCookie.Transform().SetParent2(Layer2)
	QueenCookie.Transform().SetScalef(300, 300)
	QueenCookie.Transform().SetPositionf(2000, 2000)
	QueenCookie.AddComponent(engine.NewPhysicsShape(false, chipmunk.NewCircle(vect.Vect{0, 0}, 25)))
	QueenCookie.Tag = CookieTag

	staticCookie := engine.NewGameObject("Cookie")
	staticCookie.AddComponent(engine.NewSprite(cir))
	staticCookie.Transform().SetScalef(400, 400)
	staticCookie.Transform().SetPositionf(400, 400)
	staticCookie.AddComponent(NewDestoyable(float32(engine.Inf), 2))
	staticCookie.AddComponent(engine.NewPhysicsShape(true, chipmunk.NewCircle(vect.Vect{0, 0}, 200)))

	staticCookie.Physics.Shape.SetElasticity(0)
	staticCookie.Physics.Body.SetMass(999999999999)
	staticCookie.Physics.Body.SetMoment(staticCookie.Physics.Shape.Moment(999999999999))
	staticCookie.Tag = CookieTag

	uvs, ind = engine.AnimatedGroupUVs(atlasSpace, "s")
	Background := engine.NewGameObject("Background")
	Background.AddComponent(engine.NewSprite3(atlasSpace.Texture, uvs))
	Background.Sprite.BindAnimations(ind)
	Background.Sprite.SetAnimation("s")
	Background.Sprite.AnimationSpeed = 0
	Background.Transform().SetScalef(50, 50)
	Background.Transform().SetPositionf(400, 400)

	uvs, ind = engine.AnimatedGroupUVs(atlasPowerUp, PowerUps_ID)
	PowerUpGO = engine.NewGameObject("Background")
	//PowerUpGO.Transform().SetParent2(Layer2)
	PowerUpGO.AddComponent(engine.NewSprite3(atlasPowerUp.Texture, uvs))
	PowerUpGO.AddComponent(engine.NewPhysics(false))
	PowerUpGO.Physics.Shape.IsSensor = true
	PowerUpGO.Sprite.BindAnimations(ind)
	PowerUpGO.Sprite.SetAnimation(PowerUps_ID)
	PowerUpGO.Sprite.AnimationSpeed = 0
	index := (rand.Int() % 6) + 6
	PowerUpGO.Sprite.SetAnimationIndex(int(index))
	PowerUpGO.Transform().SetScalef(61, 61)
	PowerUpGO.Transform().SetPositionf(0, 0)

	background := engine.NewGameObject("Background")
	background.AddComponent(engine.NewSprite(backgroung))
	background.Transform().SetScalef(800, 800)
	background.Transform().SetPositionf(0, 0)
	background.Transform().SetDepth(-6)
	background.Transform().SetParent2(cam)

	for i := 0; i < 300; i++ {
		c := Background.Clone()
		c.Transform().SetParent2(Layer4)
		c.Transform().SetDepth(-5)
		size := 20 + rand.Float32()*50
		p := engine.Vector{(rand.Float32() * 5000) - 1000, (rand.Float32() * 5000) - 1000, 1}

		index := rand.Int() % 7

		Background.Sprite.SetAnimationIndex(int(index))

		c.Transform().SetRotationf(rand.Float32() * 360)

		c.Transform().SetPosition(p)
		c.Transform().SetScalef(size, size)
	}

	for i := 0; i < 600; i++ {
		c := cookie.Clone()
		//c.Tag = CookieTag
		c.Transform().SetParent2(Layer2)
		size := 40 + rand.Float32()*100
		p := engine.Vector{(rand.Float32() * 4000), (rand.Float32() * 4000), 1}

		if p.X < 1100 && p.Y < 800 {
			p.X += 1100
			p.Y += 800
		}

		c.Transform().SetPosition(p)
		c.Transform().SetScalef(size, size)
	}

	Wall = engine.NewGameObject("Wall")
	Wall.Transform().SetParent2(Layer2)

	for i := 0; i < (4000/400)+2; i++ {
		c := staticCookie.Clone()
		c.Transform().SetParent2(Wall)
		p := engine.Vector{float32(i) * 400, -200, 1}
		c.Transform().SetPosition(p)
		c.Transform().SetScalef(400, 400)
	}
	for i := 0; i < (4000/400)+2; i++ {
		c := staticCookie.Clone()
		c.Transform().SetParent2(Wall)
		p := engine.Vector{float32(i) * 400, 4200, 1}
		c.Transform().SetPosition(p)
		c.Transform().SetScalef(400, 400)
	}
	for i := 0; i < (4000/400)+2; i++ {
		c := staticCookie.Clone()
		c.Transform().SetParent2(Wall)
		p := engine.Vector{-200, float32(i) * 400, 1}
		c.Transform().SetPosition(p)
		c.Transform().SetScalef(400, 400)
	}
	for i := 0; i < (4000/400)+2; i++ {
		c := staticCookie.Clone()
		c.Transform().SetParent2(Wall)
		p := engine.Vector{4200, float32(i) * 400, 1}
		c.Transform().SetPosition(p)
		c.Transform().SetScalef(400, 400)
	}

	cam.AddComponent(audio.NewAudioListener())
	clip, e := ibxm.NewClip("./data/GameSong.xm")
	if e != nil {
		panic(e)
	}
	music := engine.NewGameObject("GameSong")
	as := audio.NewAudioSource(clip)
	music.AddComponent(as)
	as.SetLooping(true)

	as.SetGain(0.3)

	s.AddGameObject(cam)
	s.AddGameObject(gui)
	s.AddGameObject(Layer1)
	s.AddGameObject(Layer2)
	s.AddGameObject(Layer3)
	s.AddGameObject(Layer4)
	s.AddGameObject(music)

	//s.AddGameObject(shadowShader)

	fmt.Println("GameScene loaded")
}
Пример #2
0
func (s *LoginScene) Load() {
	engine.SetTitle("Space Cookies")
	LoadTextures()

	rand.Seed(time.Now().UnixNano())

	LoginSceneGeneral = s

	s.Camera = engine.NewCamera()

	cam := engine.NewGameObject("Camera")
	cam.AddComponent(s.Camera)
	cam.Transform().SetPosition(engine.NewVector3(0, 0, -50))
	cam.Transform().SetScalef(1, 1)

	background := engine.NewGameObject("Background")
	background.AddComponent(engine.NewSprite(backgroundTexture))
	//background.Transform().SetScalef(float32(backgroung.Height()), float32(backgroung.Height()), 1)
	background.Transform().SetScalef(800, 800)
	background.Transform().SetPositionf(0, 0)
	background.Transform().SetDepth(-1)

	gui := engine.NewGameObject("GUI")
	gui.Transform().SetParent2(cam)

	mouse := engine.NewGameObject("Mouse")
	mouse.AddComponent(engine.NewMouse())
	mouse.Transform().SetParent2(gui)

	FPSDrawer := engine.NewGameObject("FPS")
	FPSDrawer.Transform().SetParent2(gui)
	txt := FPSDrawer.AddComponent(components.NewUIText(ArialFont2, "")).(*components.UIText)
	fps := FPSDrawer.AddComponent(engine.NewFPS()).(*engine.FPS)
	fps.SetAction(func(fps float64) {
		txt.SetString("FPS: " + strconv.FormatFloat(fps, 'f', 2, 32))
	})
	txt.SetAlign(engine.AlignLeft)

	FPSDrawer.Transform().SetPositionf((float32(-engine.Width)/2)+20, (float32(engine.Height)/2)-20)
	FPSDrawer.Transform().SetScalef(20, 20)

	/*
		tween.Create(&tween.Tween{Target: FPSDrawer, From: []float32{1}, To: []float32{100},
			Algo: tween.Linear, Type: tween.Scale, Time: time.Second * 3, Loop: tween.PingPong})

			tween.Create(&tween.Tween{Target: FPSDrawer, From: []float32{400}, To: []float32{500},
				Algo: tween.Linear, Type: tween.Position, Time: time.Second * 3, Loop: tween.PingPong, Format: "y"})

			tween.Create(&tween.Tween{Target: FPSDrawer, From: []float32{0}, To: []float32{180},
				Algo: tween.Linear, Type: tween.Rotation, Time: time.Second * 6, Loop: tween.PingPong})

		txt.SetAlign(engine.AlignCenter)
	*/
	/*
		{
			FPSDrawer := engine.NewGameObject("FPS")
			FPSDrawer.Transform().SetParent2(gui)
			txt := FPSDrawer.AddComponent(components.NewUIText(ArialFont, "")).(*components.UIText)
			fps := FPSDrawer.AddComponent(engine.NewFPS()).(*engine.FPS)
			fps.SetAction(func(fps float32) {
				txt.SetString("FPS: " + strconv.FormatFloat(float64(fps), 'f', 2, 32))
			})
			txt.SetAlign(engine.AlignLeft)

			FPSDrawer.Transform().SetPositionf(20, float32(engine.Height)-500)
			FPSDrawer.Transform().SetScalef(20, 20)
		}
	*/

	//
	tBox := engine.NewGameObject("TextBox")
	tBox.Transform().SetParent2(gui)

	txt2 := tBox.AddComponent(components.NewUIText(ArialFont2, "Type your name: ")).(*components.UIText)
	txt2.SetFocus(false)
	txt2.SetWritable(false)
	txt2.SetAlign(engine.AlignLeft)
	txt2.Transform().SetDepth(1)

	tBox.Transform().SetPositionf(-txt2.Width()*20, 0)
	tBox.Transform().SetScalef(20, 20)
	//
	input := engine.NewGameObject("TextBoxInput")
	input.Transform().SetParent2(gui)
	p := tBox.Transform().Position()
	p.X += txt2.Width() * 20
	input.Transform().SetPosition(p)
	input.Transform().SetScalef(20, 20)
	input.Transform().SetDepth(1)

	name := input.AddComponent(components.NewUIText(ArialFont2, "")).(*components.UIText)
	name.SetFocus(true)
	name.SetWritable(true)
	name.SetAlign(engine.AlignLeft)
	//
	/*
		{
			input := engine.NewGameObject("TextBoxInput")
			input.Transform().SetParent2(gui)
			p := tBox.Transform().Position()
			p.X += txt2.Width() * 20
			input.Transform().SetPosition(p)
			input.Transform().SetScalef(20, 20)

			name := input.AddComponent(components.NewUIText(ArialFont2, "")).(*components.UIText)
			name.SetFocus(true)
			name.SetWritable(true)
			name.SetAlign(engine.AlignTopCenter)
		}
		{
			input := engine.NewGameObject("TextBoxInput")
			input.Transform().SetParent2(gui)
			p := tBox.Transform().Position()
			p.X += txt2.Width() * 20
			input.Transform().SetPosition(p)
			input.Transform().SetScalef(20, 20)

			name := input.AddComponent(components.NewUIText(ArialFont2, "")).(*components.UIText)
			name.SetFocus(true)
			name.SetWritable(true)
			name.SetAlign(engine.AlignBottomRight)
		}
	*/
	//
	errLabel := engine.NewGameObject("TextBoxInput")
	errLabel.Transform().SetParent2(gui)
	errLabel.Transform().SetPositionf(0, -100)
	errLabel.Transform().SetScalef(24, 24)

	errLabelTxt := errLabel.AddComponent(components.NewUIText(ArialFont2, "")).(*components.UIText)
	errLabelTxt.SetFocus(false)
	errLabelTxt.SetWritable(false)
	errLabelTxt.SetAlign(engine.AlignCenter)
	errLabelTxt.Color = engine.Color_White
	//
	LoginButton := engine.NewGameObject("LoginButton")
	LoginButton.Transform().SetParent2(cam)
	LoginButton.Transform().SetPositionf(0, -50)
	LoginButton.AddComponent(engine.NewSprite(button))
	LoginButton.AddComponent(engine.NewPhysics(false))
	LoginButton.Physics.Shape.IsSensor = true
	LoginButton.Transform().SetScalef(50, 50)
	LoginButton.Sprite.Color = engine.Color{0.5, 0.5, 0.5, 1}
	LoginButton.Transform().SetDepth(0)

	/*
		{
			LoginButton := engine.NewGameObject("LoginButton")
			LoginButton.Transform().SetParent2(cam)
			LoginButton.Transform().SetPositionf(float32(engine.Width)/2, float32(engine.Height)/2-50)
			LoginButton.AddComponent(engine.NewSprite(button))
			LoginButton.AddComponent(engine.NewPhysics(false, 1, 1))
			LoginButton.Physics.Shape.IsSensor = true
			LoginButton.Transform().SetScalef(50, 50)
			LoginButton.Sprite.Color = engine.Vector{0.5, 0.5, 0.5}
			LoginButton.Sprite.SetAlign(engine.AlignTopLeft)
			LoginButton.AddComponent(components.NewUIButton(nil, func(enter bool) {
				if enter {
					LoginButton.Sprite.Color = engine.Vector{0.4, 0.4, 0.4}
				} else {
					LoginButton.Sprite.Color = engine.Vector{0.5, 0.5, 0.5}
				}
			}))
		}
		{
			LoginButton := engine.NewGameObject("LoginButton")
			LoginButton.Transform().SetParent2(cam)
			LoginButton.Transform().SetPositionf(float32(engine.Width)/2, float32(engine.Height)/2-50)
			LoginButton.AddComponent(engine.NewSprite(button))
			LoginButton.AddComponent(engine.NewPhysics(false, 1, 1))
			LoginButton.Physics.Shape.IsSensor = true
			LoginButton.Transform().SetScalef(50, 50)
			LoginButton.Sprite.Color = engine.Vector{0.5, 0.5, 0.5}
			LoginButton.Sprite.SetAlign(engine.AlignBottomRight)
			LoginButton.AddComponent(components.NewUIButton(nil, func(enter bool) {
				if enter {
					LoginButton.Sprite.Color = engine.Vector{0.4, 0.4, 0.4}
				} else {
					LoginButton.Sprite.Color = engine.Vector{0.5, 0.5, 0.5}
				}
			}))
		}
	*/
	loginText := engine.NewGameObject("LoginButtonText")
	loginText.Transform().SetParent2(LoginButton)
	loginText.Transform().SetWorldScalef(24, 24)
	loginText.Transform().SetPositionf(0, 0.1)
	loginText.Transform().SetDepth(1)

	if game.MyClient != nil {
		game.MyClient.Socket.Close()
		game.MyClient = nil
	}

	var errChan chan error
	LoginButton.AddComponent(components.NewUIButton(func() {
		if errChan == nil && game.MyClient == nil {
			go game.Connect(name.String(), &errChan)
			errLabelTxt.SetString("Connecting...")
		}
	}, func(enter bool) {
		if enter {
			LoginButton.Sprite.Color = engine.Color{0.4, 0.4, 0.4, 1}
		} else {
			LoginButton.Sprite.Color = engine.Color{0.5, 0.5, 0.5, 1}
		}
	}))

	txt2 = loginText.AddComponent(components.NewUIText(ArialFont2, "Log in")).(*components.UIText)
	txt2.SetFocus(false)
	txt2.SetWritable(false)
	txt2.SetAlign(engine.AlignCenter)
	txt2.Color = engine.Color{1, 1, 1, 1}
	//

	cr.Start(func() {
		for {

			if errChan == nil {
				cr.Skip()
				continue
			}
			select {
			case loginErr := <-errChan:
				if loginErr != nil {
					errLabelTxt.SetString(loginErr.Error())
					errChan = nil
				}
			default:

			}
			cr.Skip()
		}
	})

	//SPACCCEEEEE
	engine.Space.Gravity.Y = 0
	engine.Space.Iterations = 1

	//cam.Transform().SetPositionf(float32(engine.Width)/2, float32(engine.Height)/2)
	//
	cam.AddComponent(audio.NewAudioListener())
	clip, e := ibxm.NewClip("./data/LoginSong.xm")
	if e != nil {
		panic(e)
	}
	music := engine.NewGameObject("Music")
	as := audio.NewAudioSource(clip)
	music.AddComponent(as)

	s.AddGameObject(cam)
	s.AddGameObject(background)
	s.AddGameObject(music)

	fmt.Println("LoginScene loaded")
}