func (cs *CollisionSystem) Update(entity *ecs.Entity, dt float32) { var ( space *SpaceComponent collision *CollisionComponent ok bool ) if space, ok = entity.ComponentFast(space).(*SpaceComponent); !ok { return } if collision, ok = entity.ComponentFast(collision).(*CollisionComponent); !ok { return } if !collision.Main { return } var ( otherSpace *SpaceComponent otherCollision *CollisionComponent ) for _, other := range cs.Entities() { if other.ID() != entity.ID() { if otherSpace, ok = other.ComponentFast(otherSpace).(*SpaceComponent); !ok { return } if otherCollision, ok = other.ComponentFast(otherCollision).(*CollisionComponent); !ok { return } entityAABB := space.AABB() offset := Point{collision.Extra.X / 2, collision.Extra.Y / 2} entityAABB.Min.X -= offset.X entityAABB.Min.Y -= offset.Y entityAABB.Max.X += offset.X entityAABB.Max.Y += offset.Y otherAABB := otherSpace.AABB() offset = Point{otherCollision.Extra.X / 2, otherCollision.Extra.Y / 2} otherAABB.Min.X -= offset.X otherAABB.Min.Y -= offset.Y otherAABB.Max.X += offset.X otherAABB.Max.Y += offset.Y if IsIntersecting(entityAABB, otherAABB) { if otherCollision.Solid && collision.Solid { mtd := MinimumTranslation(entityAABB, otherAABB) space.Position.X += mtd.X space.Position.Y += mtd.Y } Mailbox.Dispatch(CollisionMessage{Entity: entity, To: other}) } } } }
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}) } } }
func (rs *RenderSystem) Update(entity *ecs.Entity, dt float32) { if !rs.changed { return } var render *RenderComponent var ok bool if render, ok = entity.ComponentFast(render).(*RenderComponent); !ok { return } rs.renders[render.priority] = append(rs.renders[render.priority], entity) }
func (a *AnimationSystem) Update(e *ecs.Entity, dt float32) { var ( ac *AnimationComponent r *RenderComponent ok bool ) if ac, ok = e.ComponentFast(ac).(*AnimationComponent); !ok { return } if r, ok = e.ComponentFast(r).(*RenderComponent); !ok { return } ac.change += dt if ac.change >= ac.Rate { ac.NextFrame() r.SetDrawable(ac.Cell()) } }
// Update sets the MouseComponent values for each Entity func (m *MouseSystem) Update(entity *ecs.Entity, dt float32) { var ( mc *MouseComponent space *SpaceComponent render *RenderComponent ok bool ) // We need MouseComponent to save our findings if mc, ok = entity.ComponentFast(mc).(*MouseComponent); !ok { return } // We need SpaceComponent for the location if space, ok = entity.ComponentFast(space).(*SpaceComponent); !ok { return } // We need RenderComponent for the Priority if render, ok = entity.ComponentFast(render).(*RenderComponent); !ok { return } // Reset some values mc.Leave = false mx := m.mouseX my := m.mouseY // Special case: HUD if render.priority >= HUDGround { mx = Mouse.X my = Mouse.Y } // Check if the X-value is within range // and if the Y-value is within range if mx > space.Position.X && mx < (space.Position.X+space.Width) && my > space.Position.Y && my < (space.Position.Y+space.Height) { mc.Enter = !mc.Hovered mc.Hovered = true switch Mouse.Action { case PRESS: mc.Clicked = true m.mouseDown = true case RELEASE: m.mouseDown = false mc.Dragged = false mc.Clicked = false case MOVE: if m.mouseDown { mc.Dragged = true } } } else { if mc.Hovered { mc.Leave = true } mc.Enter = false mc.Hovered = false mc.Dragged = false } }