-
Notifications
You must be signed in to change notification settings - Fork 0
/
foodSpawnerSystem.go
86 lines (72 loc) · 2.59 KB
/
foodSpawnerSystem.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
package foodzy
import (
"github.com/co0p/foodzy/asset"
"github.com/co0p/foodzy/component"
"github.com/co0p/foodzy/internal/ecs"
"github.com/hajimehoshi/ebiten/v2"
"log"
"math/rand"
)
const initialYPosition float64 = -200
var foods = []struct {
food component.Food
asset []byte
}{
{component.Food{Value: -4}, asset.Corn_baguette},
{component.Food{Value: -3}, asset.Corn_bread},
{component.Food{Value: 1}, asset.Corn_rice},
{component.Food{Value: 2}, asset.Dairy_cheese},
{component.Food{Value: 3}, asset.Dairy_milk},
{component.Food{Value: -10}, asset.Drink_beer},
{component.Food{Value: 1}, asset.Drink_coffee},
{component.Food{Value: 3}, asset.Drink_juice},
{component.Food{Value: 5}, asset.Drink_tea},
{component.Food{Value: 25}, asset.Drink_water},
{component.Food{Value: 2}, asset.Fish_crab},
{component.Food{Value: 4}, asset.Fish_sushi},
{component.Food{Value: 10}, asset.Fruit_apple},
{component.Food{Value: 10}, asset.Fruit_banana},
{component.Food{Value: 8}, asset.Fruit_grapes},
{component.Food{Value: 6}, asset.Fruit_orange},
{component.Food{Value: 2}, asset.Fruit_strawberry},
{component.Food{Value: -5}, asset.Meat_steak},
{component.Food{Value: -20}, asset.Treat_cupcake},
{component.Food{Value: -20}, asset.Treat_donut},
{component.Food{Value: 8}, asset.Vegetable_carrot},
{component.Food{Value: 6}, asset.Vegetable_eggplant},
{component.Food{Value: 2}, asset.Vegetable_potato},
{component.Food{Value: 6}, asset.Vegetable_tomato},
}
// FoodSpawningSystem is responsible for spawning random food
type FoodSpawningSystem struct {
manager *ecs.EntityManager
}
func NewFoodSpawningSystem(manager *ecs.EntityManager) *FoodSpawningSystem {
return &FoodSpawningSystem{
manager: manager,
}
}
func (s *FoodSpawningSystem) Draw(image *ebiten.Image) {}
func (s *FoodSpawningSystem) Update() error {
e := s.manager.QueryByComponents(component.FoodSpawnerType)
if len(e) != 1 {
log.Panic("expected to find exactly one spawner")
}
foodSpawner := e[0].GetComponent(component.FoodSpawnerType).(*component.FoodSpawner)
if foodSpawner.CoolDown > 0 {
foodSpawner.CoolDown--
return nil
}
padding := 10
idx := rand.Intn(len(foods))
candidate := foods[idx]
posX := float64(rand.Intn(ScreenWidth-padding*4) + padding)
posY := initialYPosition
position := component.Transform{X: posX, Y: posY, Scale: 0.7}
velocity := component.Velocity{X: foodSpawner.Velocity.X, Y: foodSpawner.Velocity.Y}
sprite := component.NewSprite("", candidate.asset)
food := NewFood(&candidate.food, sprite, &velocity, &position)
s.manager.AddEntity(food)
foodSpawner.CoolDown = foodSpawner.Rate
return nil
}