Skip to content

Commit

Permalink
Merge pull request #22 from pankona/do-not-build-on-buiding
Browse files Browse the repository at this point in the history
do not build on buiding
  • Loading branch information
pankona authored Jun 23, 2024
2 parents aba0dfc + 4cbe40c commit 3d0b378
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
5 changes: 5 additions & 0 deletions atkpane.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,10 @@ func (a *attackPane) ZIndex() int {
}

func (a *attackPane) RemoveAll() {
// 初期化順序の関係で a が nil になることがある
// TODO: みっともないので直せたら直す
if a == nil {
return
}
a.game.clickHandler.Remove(a)
}
37 changes: 37 additions & 0 deletions barricade.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type barricade struct {

// health が 0 になったときに呼ばれる関数
onDestroy func(b *barricade)

// この建物が他の建物と重なっているかどうか (建築確定前に用いるフラグ)
isOverlapping bool
}

func newBarricade(game *Game, x, y int, onDestroy func(b *barricade)) *barricade {
Expand Down Expand Up @@ -63,6 +66,15 @@ func (b *barricade) Draw(screen *ebiten.Image) {
opts := &ebiten.DrawImageOptions{}
opts.GeoM.Scale(b.scale, b.scale)
opts.GeoM.Translate(float64(b.x)-float64(b.width)*b.scale/2, float64(b.y)-float64(b.height)*b.scale/2)

// 他の建物と重なっている場合は赤くする
if b.isOverlapping {
opts.ColorScale.Scale(1, 0, 0, 1)
} else if b.game.buildCandidate == b {
// 建築確定前は暗い色で建物を描画する
opts.ColorScale.Scale(0.5, 0.5, 0.5, 1)
}

screen.DrawImage(b.image, opts)
}

Expand Down Expand Up @@ -122,3 +134,28 @@ func (b *barricade) IsClicked(x, y int) bool {
w, h := b.Size()
return b.x-w/2 <= x && x <= b.x+w/2 && b.y-h/2 <= y && y <= b.y+h/2
}

func (b *barricade) SetOverlap(overlap bool) {
b.isOverlapping = overlap
}

func (b *barricade) IsOverlap() bool {
// 他の建物と重なっているかどうかを判定する
for _, building := range b.game.buildings {
if building == b {
continue
}

bx, by := building.Position()
bw, bh := building.Size()

if intersects(
rect{b.x - b.width/2, b.y - b.height/2, b.width, b.height},
rect{bx - bw/2, by - bh/2, bw, bh},
) {
return true
}
}

return false
}
16 changes: 14 additions & 2 deletions buildpane.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func newBuildPane(game *Game) *buildPane {
return true
}

// 建築不可能な場所を指定していた場合は何もしない
if game.buildCandidate.IsOverlap() {
return false
}

// クリックされたら建築を確定する
game.AddBuilding(game.buildCandidate)
game.clickHandler.Add(game.buildCandidate)
Expand All @@ -53,8 +58,12 @@ func newBuildPane(game *Game) *buildPane {
// まだ場所が決まっていない場合はボタンを無効にする
return
}

drawRect(screen, x, y, width, height)
// 置けない場所に建築しようとした場合はボタンをグレーアウトする
if game.buildCandidate.IsOverlap() {
drawGrayRect(screen, x, y, width, height)
} else {
drawRect(screen, x, y, width, height)
}
ebitenutil.DebugPrintAt(screen, "OK", x+width/2-10, y+height/2-8)
})

Expand Down Expand Up @@ -130,6 +139,9 @@ func (a *buildPane) OnClick(x, y int) bool {

a.game.buildCandidate.SetPosition(x, y)

// 他の建築物と重なっているかどうか判定してフラグをセットする
a.game.buildCandidate.SetOverlap(a.game.buildCandidate.IsOverlap())

return true
}

Expand Down
18 changes: 18 additions & 0 deletions house.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ func (h *house) Health() int {
return h.health
}

// グレーアウトした drawRect を描画
func drawGrayRect(screen *ebiten.Image, x, y, width, height int) {
strokeWidth := float32(2)
vector.StrokeLine(screen, float32(x), float32(y), float32(x+width), float32(y), strokeWidth, color.Gray16{0x8888}, true)
vector.StrokeLine(screen, float32(x), float32(y), float32(x), float32(y+height), strokeWidth, color.Gray16{0x8888}, true)
vector.StrokeLine(screen, float32(x+width), float32(y), float32(x+width), float32(y+height), strokeWidth, color.Gray16{0x8888}, true)
vector.StrokeLine(screen, float32(x), float32(y+height), float32(x+width), float32(y+height), strokeWidth, color.Gray16{0x8888}, true)
}

func drawRect(screen *ebiten.Image, x, y, width, height int) {
strokeWidth := float32(2)
vector.StrokeLine(screen, float32(x), float32(y), float32(x+width), float32(y), strokeWidth, color.White, true)
Expand All @@ -160,3 +169,12 @@ func (h *house) IsClicked(x, y int) bool {
width, height := h.Size()
return h.x-width/2 <= x && x <= h.x+width/2 && h.y-height/2 <= y && y <= h.y+height/2
}

func (h *house) SetOverlap(overlap bool) {
// 登場の機会はないので実装しない
}

func (h *house) IsOverlap() bool {
// 登場の機会はないので実装しない
return false
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type Building interface {
Size() (int, int)
Name() string

SetOverlap(bool)
IsOverlap() bool

Clickable
Drawable
}
Expand Down

0 comments on commit 3d0b378

Please sign in to comment.