Example #1
0
func (rock *RockSpawnSystem) Update(dt float32) {
	// 4% change of spawning a rock each frame
	if rand.Float32() < .96 {
		return
	}

	position := engo.Point{
		X: rand.Float32() * engo.GameWidth(),
		Y: -32,
	}
	NewRock(rock.world, position)
}
Example #2
0
func (pong *PongGame) Setup(w *ecs.World) {
	engo.SetBackground(color.Black)
	w.AddSystem(&engo.RenderSystem{})
	w.AddSystem(&engo.CollisionSystem{})
	w.AddSystem(&engo.MouseSystem{})
	w.AddSystem(&SpeedSystem{})
	w.AddSystem(&ControlSystem{})
	w.AddSystem(&BounceSystem{})
	w.AddSystem(&ScoreSystem{})

	basicFont = (&engo.Font{URL: "Roboto-Regular.ttf", Size: 32, FG: color.NRGBA{255, 255, 255, 255}})
	if err := basicFont.CreatePreloaded(); err != nil {
		log.Fatalln("Could not load font:", err)
	}

	ballTexture := engo.Files.Image("ball.png")

	ball := Ball{BasicEntity: ecs.NewBasic()}
	ball.RenderComponent = engo.RenderComponent{
		Drawable: ballTexture,
		Scale:    engo.Point{2, 2},
	}
	ball.SpaceComponent = engo.SpaceComponent{
		Position: engo.Point{(engo.GameWidth() - ballTexture.Width()) / 2, (engo.GameHeight() - ballTexture.Height()) / 2},
		Width:    ballTexture.Width() * ball.RenderComponent.Scale.X,
		Height:   ballTexture.Height() * ball.RenderComponent.Scale.Y,
	}
	ball.CollisionComponent = engo.CollisionComponent{
		Main:  true,
		Solid: true,
	}
	ball.SpeedComponent = SpeedComponent{Point: engo.Point{300, 1000}}

	// Add our entity to the appropriate systems
	for _, system := range w.Systems() {
		switch sys := system.(type) {
		case *engo.RenderSystem:
			sys.Add(&ball.BasicEntity, &ball.RenderComponent, &ball.SpaceComponent)
		case *engo.CollisionSystem:
			sys.Add(&ball.BasicEntity, &ball.CollisionComponent, &ball.SpaceComponent)
		case *SpeedSystem:
			sys.Add(&ball.BasicEntity, &ball.SpeedComponent, &ball.SpaceComponent)
		case *BounceSystem:
			sys.Add(&ball.BasicEntity, &ball.SpeedComponent, &ball.SpaceComponent)
		}
	}

	score := Score{BasicEntity: ecs.NewBasic()}

	score.RenderComponent = engo.RenderComponent{Drawable: basicFont.Render(" ")}
	score.SpaceComponent = engo.SpaceComponent{
		Position: engo.Point{100, 100},
		Width:    100,
		Height:   100,
	}

	// Add our entity to the appropriate systems
	for _, system := range w.Systems() {
		switch sys := system.(type) {
		case *engo.RenderSystem:
			sys.Add(&score.BasicEntity, &score.RenderComponent, &score.SpaceComponent)
		case *ScoreSystem:
			sys.Add(&score.BasicEntity, &score.RenderComponent, &score.SpaceComponent)
		}
	}

	engo.Input.RegisterAxis("wasd", engo.AxisKeyPair{engo.W, engo.S})
	engo.Input.RegisterAxis("arrows", engo.AxisKeyPair{engo.ArrowUp, engo.ArrowDown})

	schemes := []string{"wasd", "arrows"}

	score.RenderComponent = engo.RenderComponent{Drawable: basicFont.Render(" ")}
	score.SpaceComponent = engo.SpaceComponent{
		Position: engo.Point{100, 100},
		Width:    100,
		Height:   100,
	}

	// Add our entity to the appropriate systems
	for _, system := range w.Systems() {
		switch sys := system.(type) {
		case *engo.RenderSystem:
			sys.Add(&score.BasicEntity, &score.RenderComponent, &score.SpaceComponent)
		case *ScoreSystem:
			sys.Add(&score.BasicEntity, &score.RenderComponent, &score.SpaceComponent)
		}
	}

	paddleTexture := engo.Files.Image("paddle.png")

	for i := 0; i < 2; i++ {
		paddle := Paddle{BasicEntity: ecs.NewBasic()}
		paddle.RenderComponent = engo.RenderComponent{
			Drawable: paddleTexture,
			Scale:    engo.Point{2, 2},
		}

		x := float32(0)
		if i != 0 {
			x = 800 - 16
		}

		paddle.SpaceComponent = engo.SpaceComponent{
			Position: engo.Point{x, (engo.GameHeight() - paddleTexture.Height()) / 2},
			Width:    paddle.RenderComponent.Scale.X * paddleTexture.Width(),
			Height:   paddle.RenderComponent.Scale.Y * paddleTexture.Height(),
		}
		paddle.ControlComponent = ControlComponent{Scheme: schemes[i]}
		paddle.CollisionComponent = engo.CollisionComponent{
			Main:  false,
			Solid: true,
		}

		// Add our entity to the appropriate systems
		for _, system := range w.Systems() {
			switch sys := system.(type) {
			case *engo.RenderSystem:
				sys.Add(&paddle.BasicEntity, &paddle.RenderComponent, &paddle.SpaceComponent)
			case *engo.CollisionSystem:
				sys.Add(&paddle.BasicEntity, &paddle.CollisionComponent, &paddle.SpaceComponent)
			case *ControlSystem:
				sys.Add(&paddle.BasicEntity, &paddle.ControlComponent, &paddle.SpaceComponent)
			}
		}
	}
}