aboutsummaryrefslogtreecommitdiff
path: root/modernworld/laser.go
blob: d2d532d6101d45fd4dd1d58e7730f57f1dc84ead (plain)
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
package invaders

import tl "github.com/JoelOtter/termloop"

type Laser struct {
	*tl.Rectangle
	Direction     int
	IsFromHero    bool
	IsNew         bool
	HasHit        bool
	HitAlienLaser bool
	Points        int
}

func NewHeroLaser(heroGunPosition int, y int) *Laser {
	return &Laser{
		Rectangle:  tl.NewRectangle(heroGunPosition, y, 1, 1, tl.ColorRed),
		Direction:  1,
		IsNew:      true,
		IsFromHero: true,
	}
}

func NewAlienLaser(alienGunPosition int, y int) *Laser {
	return &Laser{
		Rectangle:  tl.NewRectangle(alienGunPosition, y, 1, 1, tl.ColorGreen),
		Direction:  -1,
		IsNew:      true,
		IsFromHero: false,
		Points:     5,
	}
}

func (laser *Laser) Collide(collision tl.Physical) {
	if laser.IsFromHero == false {
		return
	}

	if laserCollide, isLaser := collision.(*Laser); isLaser {
		laser.HasHit = true
		laser.HitAlienLaser = true
		laser.Points = laserCollide.Points

		laserCollide.HasHit = true
	}
}