-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsys_item.go
84 lines (74 loc) · 2.16 KB
/
sys_item.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
package kar
import (
"kar/items"
"kar/res"
"github.com/mlange-42/ark/ecs"
)
type Item struct {
toRemoveComponent []ecs.Entity
itemHit *HitInfo
}
func (i *Item) Init() {
i.itemHit = &HitInfo{}
}
func (i *Item) Update() {
q := filterCollisionDelayer.Query()
for q.Next() {
delayer := q.Get()
delayer.Duration -= Tick
}
// dropped items collisions and animations
if world.Alive(currentPlayer) {
if gameDataRes.GameplayState == Playing {
playerBox := mapAABB.GetUnchecked(currentPlayer)
itemQuery := filterDroppedItem.Query()
for itemQuery.Next() {
itemID, itemPos, timers := itemQuery.Get()
dropItemAABB.Pos = *(*Vec)(itemPos)
itemEntity := itemQuery.Entity()
if !mapCollisionDelayer.HasUnchecked(itemEntity) {
// Check player-item collision
if Overlap(playerBox, dropItemAABB, i.itemHit) {
// if Durability component exists, get durability
dur := 0
if mapDurability.HasUnchecked(itemEntity) {
dur = mapDurability.GetUnchecked(itemEntity).Durability
}
if inventoryRes.AddItemIfEmpty(itemID.ID, dur) {
toRemove = append(toRemove, itemEntity)
}
onInventorySlotChanged()
}
} else {
if mapCollisionDelayer.GetUnchecked(itemEntity).Duration < 0 {
i.toRemoveComponent = append(i.toRemoveComponent, itemEntity)
}
}
dropItemAABB.Pos.Y += 6
// vertical item sine animation
tileCollider.Collisions = tileCollider.Collisions[:0]
dy := tileCollider.CollideY(dropItemAABB, ItemGravity)
itemPos.Y += dy
timers.Index = (timers.Index + 1) % len(sinspace)
}
}
}
// Remove MapCollisionDelayer components
for _, entity := range i.toRemoveComponent {
mapCollisionDelayer.Remove(entity)
}
i.toRemoveComponent = i.toRemoveComponent[:0]
}
func (i *Item) Draw() {
// Draw drop Items
itemQuery := filterDroppedItem.Query()
for itemQuery.Next() {
id, pos, animIndex := itemQuery.Get()
dropItemAABB.Pos = *(*Vec)(pos)
colorMDIO.GeoM.Reset()
colorMDIO.GeoM.Translate(dropItemAABB.Left(), dropItemAABB.Top()+sinspace[animIndex.Index])
if id.ID != items.Air {
cameraRes.DrawWithColorM(res.Icon8[id.ID], colorM, colorMDIO, Screen)
}
}
}