<commit_before>// © 2012 the Minima Authors under the MIT license. See AUTHORS for the list of authors.

package main

import (
	"code.google.com/p/min-game/geom"
	"code.google.com/p/min-game/item"
	"code.google.com/p/min-game/ui"
)

type Base struct {
	Box geom.Rectangle

	Storage Inventory
}

func NewBase(p geom.Point) Base {
	return Base{
		Box: geom.Rectangle{
			Min: p,
			Max: p.Add(geom.Pt(64, 64)),
		},
		Storage: Inventory{[]*item.Item{item.New(item.ETele)}, 0, false},
	}
}

func (b *Base) Draw(d ui.Drawer, cam ui.Camera) {
	cam.Draw(d, ui.Sprite{
		Name:   "Base",
		Bounds: geom.Rect(0, 0, b.Box.Dx(), b.Box.Dy()),
		Shade:  1.0,
	}, b.Box.Min)
}

// PutStorage adds i to the base's storage.
func (b *Base) PutStorage(i *item.Item) {
	b.Storage.Put(i)
}

type BaseScreen struct {
	astro   *Player
	base    *Base
	closing bool

	inPack   bool
	selected int
}

const pad = 4

var origin = geom.Pt(32, 32)
var bounds = geom.Rectangle{
	Min: origin,
	Max: origin.Add(geom.Pt(ScreenDims.X, ScreenDims.Y/2)).Sub(origin.Mul(2)),
}
var packBounds = bounds.Add(geom.Pt(0, bounds.Dy()+3*pad+32))

func NewBaseScreen(astro *Player, base *Base) *BaseScreen {
	return &BaseScreen{astro, base, false, false, 0}
}

func (s *BaseScreen) Transparent() bool {
	return true
}

func (s *BaseScreen) Draw(d ui.Drawer) {
	d.SetFont(DialogFont, 16)
	pt := DrawInventory(BaseInv{s, "Pack"}, d, pad, origin, true)
	DrawInventory(BaseInv{s, "Storage"}, d, pad, geom.Pt(origin.X, pt.Y+32+2*pad), false)
}

type BaseInv struct {
	s     *BaseScreen
	label string
}

func (b BaseInv) Label() string {
	return b.label
}

func (b BaseInv) Len() int {
	if b.label == "Pack" {
		return b.s.astro.pack.Len()
	}
	return b.s.base.Storage.Len()
}

func (b BaseInv) Selected(n int) bool {
	if b.label == "Pack" && b.s.inPack || b.label == "Storage" && !b.s.inPack {
		return b.s.selected == n
	}
	return false
}

func (b BaseInv) Get(n int) *item.Item {
	if b.label == "Pack" {
		return b.s.astro.pack.Get(n)
	}
	return b.s.base.Storage.Get(n)
}

func (b BaseInv) Set(n int, i *item.Item) {
	if b.label == "Pack" {
		b.s.astro.pack.Set(n, i)
	} else {
		b.s.base.Storage.Set(n, i)
	}
}

func (s *BaseScreen) Handle(stk *ui.ScreenStack, e ui.Event) error {
	if s.closing {
		return nil
	}

	key, ok := e.(ui.Key)
	if !ok || !key.Down {
		return nil
	}

	switch key.Button {
	case ui.Menu:
		s.closing = true
	case ui.Action:
		if s.inPack && s.astro.pack.Get(s.selected) != nil {
			i := s.astro.pack.Get(s.selected)
			s.astro.pack.Set(s.selected, nil)
			s.base.PutStorage(i)
		}
		if !s.inPack && s.base.Storage.Get(s.selected) != nil {
			i := s.base.Storage.Get(s.selected)
			if s.astro.PutPack(i) {
				s.base.Storage.Set(s.selected, nil)
			}
		}
	case ui.Left:
		s.selected--
		if s.selected < 0 {
			if s.inPack {
				s.selected = s.astro.pack.Len() - 1
			} else {
				s.selected = s.base.Storage.Len() - 1
			}
		}
	case ui.Right:
		s.selected++
		if s.inPack && s.selected == s.astro.pack.Len() {
			s.selected = 0
		}
		if !s.inPack && s.selected == s.base.Storage.Len() {
			s.selected = 0
		}
	case ui.Up, ui.Down:
		s.inPack = !s.inPack
		if s.inPack && s.selected >= s.astro.pack.Len() {
			s.selected = s.astro.pack.Len() - 1
		}
		if !s.inPack && s.selected >= s.base.Storage.Len() {
			s.selected = s.base.Storage.Len() - 1
		}
	}<commit_msg>Use the Inventory's Storage field in the base, instead of a custom one.<commit_after>// © 2012 the Minima Authors under the MIT license. See AUTHORS for the list of authors.

package main

import (
	"code.google.com/p/min-game/geom"
	"code.google.com/p/min-game/item"
	"code.google.com/p/min-game/ui"
)

type Base struct {
	Box geom.Rectangle

	Storage Inventory
}

func NewBase(p geom.Point) Base {
	return Base{
		Box: geom.Rectangle{
			Min: p,
			Max: p.Add(geom.Pt(64, 64)),
		},
		Storage: Inventory{[]*item.Item{item.New(item.ETele)}, 0, false},
	}
}

func (b *Base) Draw(d ui.Drawer, cam ui.Camera) {
	cam.Draw(d, ui.Sprite{
		Name:   "Base",
		Bounds: geom.Rect(0, 0, b.Box.Dx(), b.Box.Dy()),
		Shade:  1.0,
	}, b.Box.Min)
}

// PutStorage adds i to the base's storage.
func (b *Base) PutStorage(i *item.Item) {
	b.Storage.Put(i)
}

type BaseScreen struct {
	astro   *Player
	base    *Base
	closing bool

	inPack bool
}

const pad = 4

var origin = geom.Pt(32, 32)
var bounds = geom.Rectangle{
	Min: origin,
	Max: origin.Add(geom.Pt(ScreenDims.X, ScreenDims.Y/2)).Sub(origin.Mul(2)),
}
var packBounds = bounds.Add(geom.Pt(0, bounds.Dy()+3*pad+32))

func NewBaseScreen(astro *Player, base *Base) *BaseScreen {
	return &BaseScreen{astro, base, false, false}
}

func (s *BaseScreen) Transparent() bool {
	return true
}

func (s *BaseScreen) Draw(d ui.Drawer) {
	d.SetFont(DialogFont, 16)
	pt := s.astro.pack.Draw("Pack", d, pad, origin, true)
	s.base.Storage.Draw("Storage", d, pad, geom.Pt(origin.X, pt.Y+32+2*pad), false)
}

func (s *BaseScreen) Handle(stk *ui.ScreenStack, e ui.Event) error {
	if s.closing {
		return nil
	}

	key, ok := e.(ui.Key)
	if !ok || !key.Down {
		return nil
	}

	switch key.Button {
	case ui.Menu:
		s.closing = true
	case ui.Action:
		if s.inPack && s.astro.pack.Get(s.astro.pack.Selected) != nil {
			i := s.astro.pack.Get(s.astro.pack.Selected)
			s.astro.pack.Set(s.astro.pack.Selected, nil)
			s.base.PutStorage(i)
		}
		if !s.inPack && s.base.Storage.Get(s.base.Storage.Selected) != nil {
			i := s.base.Storage.Get(s.base.Storage.Selected)
			if s.astro.PutPack(i) {
				s.base.Storage.Set(s.base.Storage.Selected, nil)
			}
		}
	case ui.Left:
		if s.inPack {
			s.astro.pack.Selected--
			if s.astro.pack.Selected < 0 {
				s.astro.pack.Selected = s.astro.pack.Len() - 1
			}
		} else {
			s.base.Storage.Selected--
			if s.base.Storage.Selected < 0 {
				s.astro.pack.Selected = s.base.Storage.Len() - 1
			}
		}
	case ui.Right:
		if s.inPack {
			s.astro.pack.Selected++
			if s.astro.pack.Selected == s.astro.pack.Len() {
				s.astro.pack.Selected = 0
			}
		} else {
			s.base.Storage.Selected++
			if s.base.Storage.Selected == s.base.Storage.Len() {
				s.base.Storage.Selected = 0
			}
		}
	case ui.Up, ui.Down:
		if s.inPack {
			if s.base.Storage.Selected >= s.astro.pack.Len() {
				s.astro.pack.Selected = s.astro.pack.Len() - 1
			}
			s.inPack = false
		} else {
			if s.astro.pack.Selected >= s.base.Storage.Len() {
				s.base.Storage.Selected = s.base.Storage.Len() - 1
			}
			s.inPack = true
		}
	}
{'fraction_non_alphanumeric': 0.1751836224410064, 'fraction_numerical': 0.011564306922956713, 'mean_word_length': 2.972687771570453, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5850295', 'n_tokens_mistral': 2899, 'n_tokens_neox': 2647, 'n_words': 582}