-
Notifications
You must be signed in to change notification settings - Fork 11
/
tracebox.go
141 lines (130 loc) · 3.8 KB
/
tracebox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package engine
import (
"github.com/divVerent/aaaaxy/internal/level"
"github.com/divVerent/aaaaxy/internal/log"
m "github.com/divVerent/aaaaxy/internal/math"
)
func (l *normalizedLine) traceBoxTiles(w *World, o TraceOptions, enlarge m.Delta, result *TraceResult) {
if o.PathOut != nil {
*o.PathOut = append(*o.PathOut, l.Origin.Div(level.TileSize))
}
// Find the corner in direction of the trace.
var adjustment m.Delta
if l.XDir > 0 {
adjustment.DX = enlarge.DX
}
if l.YDir > 0 {
adjustment.DY = enlarge.DY
}
// Trace that corner path against the tilemap.
prevOrigin := l.Origin
prevTarget := l.Target
l.Origin = l.Origin.Add(adjustment)
l.Target = l.Target.Add(adjustment)
l.walkTiles(func(prevTile, nextTile m.Pos, delta m.Delta, prevPixelAdj, nextPixelAdj m.Pos) error {
// First, unadjust.
prevPixel := prevPixelAdj.Sub(adjustment)
nextPixel := nextPixelAdj.Sub(adjustment)
// Check the newly hit tiles.
if nextPixel.X != prevPixel.X {
// X move.
// Check all newly hit tiles in Y range.
top := m.Div(nextPixel.Y, level.TileSize)
bottom := m.Div(nextPixel.Y+enlarge.DY, level.TileSize)
for y := top; y <= bottom; y++ {
tilePos := m.Pos{X: nextTile.X, Y: y}
var tile *level.Tile
if o.LoadTiles {
tile = w.LoadTile(tilePos.Sub(delta), tilePos, delta)
} else {
tile = w.Tile(tilePos)
}
if tile == nil {
result.EndPos = prevPixel
result.HitDelta = delta
// result.HitFogOfWar = true
return errTraceDone
}
if o.Contents&tile.Contents != 0 {
result.EndPos = prevPixel
result.HitDelta = delta
// result.HitTilePos = nextTile
// result.HitTile = tile
return errTraceDone
}
}
} else {
// Y move.
// Check all newly hit tiles in X range.
left := m.Div(nextPixel.X, level.TileSize)
right := m.Div(nextPixel.X+enlarge.DX, level.TileSize)
for x := left; x <= right; x++ {
tilePos := m.Pos{X: x, Y: nextTile.Y}
var tile *level.Tile
if o.LoadTiles {
tile = w.LoadTile(tilePos.Sub(delta), tilePos, delta)
} else {
tile = w.Tile(tilePos)
}
if tile == nil {
result.EndPos = prevPixel
result.HitDelta = delta
// result.HitFogOfWar = true
return errTraceDone
}
if o.Contents&tile.Contents != 0 {
result.EndPos = prevPixel
result.HitDelta = delta
// result.HitTilePos = nextTile
// result.HitTile = tile
return errTraceDone
}
}
}
return nil
})
l.Origin = prevOrigin
l.Target = prevTarget
}
func traceBox(w *World, from m.Rect, to m.Pos, o TraceOptions) TraceResult {
if o.Contents == level.NoContents {
log.Fatalf("do not know what to stop at - need to specify Contents in every trace")
}
result := TraceResult{
EndPos: to,
// HitTile: nil,
// HitEntities: nil,
// HitFogOfWar: false,
}
if from.Origin == to {
// Empty trace? Nothign we can hit.
return result
}
l := normalizeLine(from.Origin, to)
// As from != to, we know NumSteps > 0.
enlarge := from.Size.Sub(m.Delta{DX: 1, DY: 1})
maxBorder := 0
if o.ForEnt != nil {
maxBorder = o.ForEnt.BorderPixels
}
if !o.NoTiles {
l.traceBoxTiles(w, o, enlarge, &result)
}
if !o.NoEntities {
l.traceEntities(w, o, enlarge, maxBorder, &result)
}
return result
}