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
|
package modernworld
import (
_ "embed"
tl "github.com/JoelOtter/termloop"
)
type Alien struct {
*tl.Entity
IsAlive bool
IsRendered bool
Points int
}
type AlienType struct {
Source []byte
Points int
}
var (
//go:embed files/alien_basic.txt
alienBasicBytes []byte
//go:embed files/alien_medium.txt
alienMediumBytes []byte
//go:embed files/alien_strong.txt
alienStrongBytes []byte
Basic = AlienType{Source: alienBasicBytes, Points: 10}
Medium = AlienType{Source: alienMediumBytes, Points: 20}
Strong = AlienType{Source: alienStrongBytes, Points: 30}
)
func NewAlien(alienType AlienType) *Alien {
canvas := CreateCanvas(alienType.Source)
return &Alien{Entity: tl.NewEntityFromCanvas(0, 0, canvas), IsAlive: true, Points: alienType.Points}
}
func CreateAliensLine(alienType AlienType, lineSize int) []*Alien {
aliens := make([]*Alien, lineSize)
for i := 0; i < lineSize; i++ {
aliens[i] = NewAlien(alienType)
}
return aliens
}
func SetPositionAndRenderAliens(aliens [][]*Alien, level *tl.BaseLevel, arena *Arena) {
initialX, initialY, space := calcInitialPositionAndSpace(aliens, arena)
for index, line := range aliens {
x := initialX
for _, alien := range line {
_, height := alien.Size()
y := initialY + height*(index+1) - 2
alien.SetPosition(x, y)
alien.IsRendered = true
level.AddEntity(alien)
x += space
}
}
}
func calcInitialPositionAndSpace(aliens [][]*Alien, arena *Arena) (int, int, int) {
lineSize := len(aliens[0])
alienW, _ := aliens[0][0].Size()
space := alienW + 1
arenaX, arenaY := arena.Position()
arenaW, _ := arena.Size()
totalWidth := lineSize * space
x := arenaX + arenaW/2 - totalWidth/2
return x, arenaY, space
}
func (alien *Alien) Collide(collision tl.Physical) {
if _, ok := collision.(*Laser); ok {
laser := collision.(*Laser)
if laser.IsFromHero {
laser.HasHit = true
alien.IsAlive = false
}
}
}
|