|
| 1 | +package ui2d |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/jackmott/rpg/game" |
| 6 | + "github.com/veandco/go-sdl2/sdl" |
| 7 | +) |
| 8 | + |
| 9 | +func (ui *ui) DrawInventory(level *game.Level) { |
| 10 | + |
| 11 | + playerSrcRect := ui.textureIndex[level.Player.Rune][0] |
| 12 | + invRect := ui.getInventoryRect() |
| 13 | + ui.renderer.Copy(ui.groundInventoryBackground, nil, invRect) |
| 14 | + offset := int32(float64(invRect.H) * 0.05) |
| 15 | + |
| 16 | + ui.renderer.Copy(ui.textureAtlas, &playerSrcRect, &sdl.Rect{invRect.X + invRect.X/4, invRect.Y + offset, invRect.W / 2, invRect.H / 2}) |
| 17 | + ui.renderer.Copy(ui.slotBackground, nil, ui.getHelmetSlotRect()) |
| 18 | + if level.Player.Helmet != nil { |
| 19 | + ui.renderer.Copy(ui.textureAtlas, &ui.textureIndex[level.Player.Helmet.Rune][0], ui.getHelmetSlotRect()) |
| 20 | + } |
| 21 | + ui.renderer.Copy(ui.slotBackground, nil, ui.getWeaponSlotRect()) |
| 22 | + if level.Player.Weapon != nil { |
| 23 | + ui.renderer.Copy(ui.textureAtlas, &ui.textureIndex[level.Player.Weapon.Rune][0], ui.getWeaponSlotRect()) |
| 24 | + } |
| 25 | + |
| 26 | + for i, item := range level.Player.Items { |
| 27 | + itemSrcRect := ui.textureIndex[item.Rune][0] |
| 28 | + if item == ui.draggedItem { |
| 29 | + itemSize := int32(ItemSizeRatio * float32(ui.winWidth)) |
| 30 | + ui.renderer.Copy(ui.textureAtlas, &itemSrcRect, &sdl.Rect{int32(ui.currentMouseState.pos.X), int32(ui.currentMouseState.pos.Y), itemSize, itemSize}) |
| 31 | + } else { |
| 32 | + ui.renderer.Copy(ui.textureAtlas, &itemSrcRect, ui.getInventoryItemRect(i)) |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (ui *ui) getHelmetSlotRect() *sdl.Rect { |
| 38 | + invRect := ui.getInventoryRect() |
| 39 | + slotSize := int32(ItemSizeRatio * float32(ui.winWidth) * 1.05) |
| 40 | + |
| 41 | + r := sdl.Rect{(invRect.X*2+invRect.W)/2 - slotSize/2, invRect.Y, slotSize, slotSize} |
| 42 | + fmt.Println("x:", r.X, "y:", r.Y, "w:", r.W, "h:", r.H) |
| 43 | + return &r |
| 44 | +} |
| 45 | + |
| 46 | +func (ui *ui) getWeaponSlotRect() *sdl.Rect { |
| 47 | + invRect := ui.getInventoryRect() |
| 48 | + slotSize := int32(ItemSizeRatio * float32(ui.winWidth) * 1.05) |
| 49 | + yoffset := int32(float32(invRect.H) * 0.18) |
| 50 | + xoffset := int32(float32(invRect.W) * 0.18) |
| 51 | + r := sdl.Rect{invRect.X + xoffset, invRect.Y + yoffset, slotSize, slotSize} |
| 52 | + fmt.Println("x:", r.X, "y:", r.Y, "w:", r.W, "h:", r.H) |
| 53 | + return &r |
| 54 | +} |
| 55 | + |
| 56 | +func (ui *ui) getInventoryRect() *sdl.Rect { |
| 57 | + invWidth := int32(float32(ui.winWidth) * 0.40) |
| 58 | + invHeight := int32(float32(ui.winHeight) * 0.75) |
| 59 | + offsetX := (int32(ui.winWidth) - invWidth) / 2 |
| 60 | + offsetY := (int32(ui.winHeight) - invHeight) / 2 |
| 61 | + return &sdl.Rect{offsetX, offsetY, invWidth, invHeight} |
| 62 | +} |
| 63 | + |
| 64 | +func (ui *ui) getInventoryItemRect(i int) *sdl.Rect { |
| 65 | + invRect := ui.getInventoryRect() |
| 66 | + itemSize := int32(ItemSizeRatio * float32(ui.winWidth)) |
| 67 | + return &sdl.Rect{invRect.X + int32(i)*itemSize, invRect.Y + invRect.H - itemSize, itemSize, itemSize} |
| 68 | +} |
| 69 | + |
| 70 | +func (ui *ui) CheckEquippedItem() *game.Item { |
| 71 | + mousePos := ui.currentMouseState.pos |
| 72 | + if ui.draggedItem.Typ == game.Weapon { |
| 73 | + r := ui.getWeaponSlotRect() |
| 74 | + if r.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) { |
| 75 | + return ui.draggedItem |
| 76 | + } |
| 77 | + } else if ui.draggedItem.Typ == game.Helmet { |
| 78 | + r := ui.getHelmetSlotRect() |
| 79 | + if r.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) { |
| 80 | + return ui.draggedItem |
| 81 | + } |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (ui *ui) CheckDroppedItem() *game.Item { |
| 87 | + invRect := ui.getInventoryRect() |
| 88 | + mousePos := ui.currentMouseState.pos |
| 89 | + if invRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) { |
| 90 | + return nil |
| 91 | + } |
| 92 | + return ui.draggedItem |
| 93 | +} |
| 94 | + |
| 95 | +func (ui *ui) CheckInventoryItems(level *game.Level) *game.Item { |
| 96 | + if ui.currentMouseState.leftButton { |
| 97 | + mousePos := ui.currentMouseState.pos |
| 98 | + for i, item := range level.Player.Items { |
| 99 | + itemRect := ui.getInventoryItemRect(i) |
| 100 | + if itemRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) { |
| 101 | + return item |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (ui *ui) CheckGroundItems(level *game.Level) *game.Item { |
| 109 | + if !ui.currentMouseState.leftButton && ui.prevMouseState.leftButton { |
| 110 | + items := level.Items[level.Player.Pos] |
| 111 | + mousePos := ui.currentMouseState.pos |
| 112 | + for i, item := range items { |
| 113 | + itemRect := ui.getGroundItemRect(i) |
| 114 | + if itemRect.HasIntersection(&sdl.Rect{int32(mousePos.X), int32(mousePos.Y), 1, 1}) { |
| 115 | + return item |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
0 commit comments