func ExampleGetRouteBetween_point() { from := &terrain.Position{0, 0} to := &terrain.Position{0, 0} route := terrain.GetRouteBetween(from, to) fmt.Println(route) // Output: [[0 0]] }
func ExampleGetRouteBetween_other() { from := &terrain.Position{-1, 5} to := &terrain.Position{5, 0} route := terrain.GetRouteBetween(from, to) fmt.Println(route) // Output: [[-1 5] [0 4] [1 3] [2 3] [3 2] [4 1] [5 0]] }
func ExampleGetRouteBetween_horizontal() { from := &terrain.Position{-2, 0} to := &terrain.Position{4, 0} route := terrain.GetRouteBetween(from, to) fmt.Println(route) // Output: [[-2 0] [-1 0] [0 0] [1 0] [2 0] [3 0] [4 0]] }
func ExampleGetRouteBetween_diagonal() { from := &terrain.Position{-2, 2} to := &terrain.Position{2, -2} route := terrain.GetRouteBetween(from, to) fmt.Println(route) // Output: [[-2 2] [-1 1] [0 0] [1 -1] [2 -2]] }
// Compute an entity's new position. // Returns an EntityDiff if the entity has changed, nil otherwise. func (m *Mover) UpdateEntity(ent *entity.Entity, now message.AbsoluteTick) (req *entity.UpdateRequest, collidesWith interface{}) { // TODO: remove Mover.lastUpdates? var last message.AbsoluteTick var ok bool if last, ok = m.lastUpdates[ent.Id]; !ok { last = now } m.lastUpdates[ent.Id] = now dt := time.Duration(now-last) * clock.TickDuration // Convert to seconds if dt == 0 { return } if dt < 0 { return // TODO: figure out if it's the right thing to do here } speed := ent.Speed pos := ent.Position nextPos := speed.GetNextPosition(pos, dt) if nextPos == nil { return } // Check terrain // TODO: use brensenham algorithm // See http://tech-algorithm.com/articles/drawing-line-using-bresenham-algorithm/ route := terrain.GetRouteBetween(pos, nextPos) stoppedAt, collidesWith := checkRoute(route, ent, m.engine.ctx.Terrain, m.engine.ctx.Entity.List()) if stoppedAt != nil { // The entity has been stopped while moving nextPos = stoppedAt } newEnt := entity.New() newEnt.Id = ent.Id newEnt.Position = nextPos diff := &message.EntityDiff{Position: true} req = entity.NewUpdateRequest(now, newEnt, diff) return }