Esempio n. 1
0
func New() *Core {
	defer debug.Trace().UnTrace()

	core := &Core{}
	core.State = Running
	return core
}
Esempio n. 2
0
// Shutdown deinits all spaces and then deinits all managers (in LIFO order)
func (c *Core) ShutDown() {
	defer debug.Trace().UnTrace()

	for i := len(c.spaces) - 1; i >= 0; i-- {
		c.spaces[i].DeInit()
	}

	for i := len(c.managers) - 1; i >= 0; i-- {
		c.managers[i].ShutDown()
	}
}
Esempio n. 3
0
// StartUp passes an initliazer to all managers on start up, before intializing
// all registered spaces.
//
func (c *Core) StartUp(config GameObject) {
	defer debug.Trace().UnTrace()

	for _, m := range c.managers {
		m.StartUp(config)
	}

	for _, s := range c.spaces {
		s.Init()
	}
}
Esempio n. 4
0
// LoadConfig serialize in a GameObject from a text file.
func LoadConfig(file string) GameObject {
	defer debug.Trace().UnTrace()

	data, err := support.OpenFile(file)
	if err != nil {
		support.LogFatal("Failed to open Config file: " + file)
	}

	holder, err := support.ReadData(data)
	if err != nil {
		support.LogFatal("Failed to read in Config file: " + file)
		return nil
	}

	goc := Goc{}
	SerializeInPlace(&goc, holder)
	return &goc
}
Esempio n. 5
0
// s := f.Create(f1, Name("space"))
// goc1 := f.Create(f2, Owner(s), Name("goc1"))
// goc2 := f.Create(f2, Owner(s), Dispatcher(d))
func (f *Factory) Create(file string, opts ...FactoryOpt) types.JGameObject {
	defer debug.Trace().UnTrace()

	// read in game object data

	data, err := support.OpenFile(file)
	if err != nil {
		support.LogFatal("Failed to open Config file: " + file)
	}

	holder, err := support.ReadData(data)
	if err != nil {
		support.LogFatal("Failed to read in Config file: " + file)
		return nil
	}

	m := v.(map[string]interface{})
	typename, err := m["Type"]
	if err != nil {
		log.Panic(err)
	}
	obj := factoryFunc(typename.(string))
	SerializeInPlace(obj, holder)

	// apply options to game object
	for _, opt := range opts {
		opt(obj)
	}

	// check for transform, check for dispatcher, name etc

	if obj.Name() == "" {
		obj.SetName(fmt.Sprint("obj", len(f.ObjList)))
	}

	// add game object to factory

	f.ObjList = append(f.ObjList, obj)
	return obj
	// h := types.JGameObject{id: len(f.ObjList)}
	// f.NameMap[obj.Name()] = h
	// return h
}
Esempio n. 6
0
func (c *Core) Run() utils.ReturnCode {
	defer debug.Trace().UnTrace()

	framesPerSec := time.Duration(int(1e9) / 30)
	clk := time.NewTicker(framesPerSec)
	prevTime := time.Now()

UpdateLoop:
	for {
		select {
		case currTime := <-clk.C:
			if c.State != Running && c.State != Stopped {
				break UpdateLoop
			}

			c.GameData.CurrFrame++

			for _, mgr := range c.managers {
				mgr.BeginFrame()
				defer mgr.EndFrame()
			}

			for _, spc := range c.spaces {
				dt := float32(currTime.Sub(prevTime).Seconds())
				spc.Update(dt)
			}

			// TODO: make a public stopped channel, once gamecore is public
			if c.State == Stopped {
				break UpdateLoop
			}

			prevTime = currTime
		}
	}

	switch c.State {
	case Rebooting:
		return utils.ES_Restart
	}

	return utils.ES_Success
}
Esempio n. 7
0
func (c *core) Step() {
	defer debug.Trace().UnTrace()

	if c.TimeStamp.IsZero() {
		c.TimeStamp = time.Now()
	}

	if dt := time.Since(c.TimeStamp); dt >= 16*time.Millisecond {
		fmt.Println("frame:", c.CurrFrame)
		c.CurrFrame++

		for _, mgr := range c.managers {
			mgr.BeginFrame()
			defer mgr.EndFrame()
		}

		for _, spc := range c.spaces {
			spc.Update(float32(dt))
		}
		fmt.Println()
	}
}