Ejemplo n.º 1
0
func (a *MovementSystem) Update(entity *ecs.Entity, dt float32) {
	var move *MovementComponent
	if !entity.Component(&move) {
		return
	}

	if !move.started {
		move.started = true
		move.timeLeft = float32(move.In.Seconds())
		move.speedX = (move.From.X - move.To.X) / move.timeLeft
		move.speedY = (move.From.Y - move.To.Y) / move.timeLeft
	}
	move.timeLeft -= dt

	var space *engi.SpaceComponent
	if !entity.Component(&space) {
		return
	}

	space.Position.X -= move.speedX * dt
	space.Position.Y -= move.speedY * dt

	if move.timeLeft < 0 {
		// Because we might move more than needed
		space.Position.X -= move.speedX * move.timeLeft
		space.Position.Y -= move.speedY * move.timeLeft
		move.timeLeft = 0
	}

	if move.timeLeft == 0 {
		entity.RemoveComponent(move)
		move.Callback()
	}
}
Ejemplo n.º 2
0
func (as *AudioSystem) Update(entity *ecs.Entity, dt float32) {
	var ac *AudioComponent
	var ok bool
	if ac, ok = entity.ComponentFast(ac).(*AudioComponent); !ok {
		return
	}

	if ac.player == nil {
		f := Files.Sound(ac.File)
		if f == nil {
			return
		}

		var err error
		ac.player, err = NewPlayer(f, 0, 0)
		if err != nil {
			log.Println("Error initializing AudioSystem:", err)
			return
		}
	}

	if ac.player.State() != Playing {
		if ac.player.State() == Stopped {
			if !ac.Repeat {
				al.RewindSources(ac.player.source)
				al.StopSources(ac.player.source)
				entity.RemoveComponent(ac)
				return
			}
		}

		// Prepares if the track hasn't been buffered before.
		if err := ac.player.prepare(ac.Background, 0, false); err != nil {
			log.Println("Error initializing AudioSystem:", err)
			return
		}

		al.PlaySources(ac.player.source)

		if !ac.Background {
			var space *SpaceComponent
			var ok bool
			if space, ok = entity.ComponentFast(space).(*SpaceComponent); !ok {
				return
			}

			ac.player.source.SetPosition(al.Vector{
				(space.Position.X + space.Width/2) / Width(),
				(space.Position.Y + space.Height/2) / Height(),
				0})
		}
	}
}