示例#1
0
func CreateWatermelon(pg *playground.Playground, cxt context.Context,
) (*Watermelon, error) {

	if pg == nil {
		return nil, &errCreateObject{playground.ErrNilPlayground}
	}
	if err := cxt.Err(); err != nil {
		return nil, &errCreateObject{err}
	}

	dots, err := pg.GetEmptyField(_WATERMELON_W, _WATERMELON_H)
	if err != nil {
		return nil, &errCreateObject{err}
	}

	wcxt, cncl := context.WithCancel(cxt)
	watermelon := &Watermelon{
		pg:      pg,
		updated: time.Now(),
		cancel:  cncl,
	}
	copy(watermelon.dots[:], dots[:_WATERMELON_AREA])

	if err := pg.Locate(watermelon); err != nil {
		return nil, &errCreateObject{err}
	}

	if err := watermelon.run(wcxt); err != nil {
		pg.Delete(watermelon)
		return nil, &errCreateObject{err}
	}

	return watermelon, nil
}
示例#2
0
// Corpses are created when a snake dies
func CreateCorpse(pg *playground.Playground, cxt context.Context,
	dots playground.DotList) (*Corpse, error) {

	if pg == nil {
		return nil, &errCreateObject{playground.ErrNilPlayground}
	}
	if len(dots) == 0 {
		return nil, &errCreateObject{playground.ErrEmptyDotList}
	}
	if err := cxt.Err(); err != nil {
		return nil, &errCreateObject{err}
	}

	ccxt, cancel := context.WithCancel(cxt)
	corpse := &Corpse{pg, dots, time.Now(), nil, cancel}

	if err := pg.Locate(corpse); err != nil {
		return nil, &errCreateObject{err}
	}

	if err := corpse.run(ccxt); err != nil {
		pg.Delete(corpse)
		return nil, &errCreateObject{err}
	}

	return corpse, nil
}
示例#3
0
// CreateSnake creates new snake
func CreateSnake(pg *playground.Playground, cxt context.Context,
) (*Snake, error) {

	if pg == nil {
		return nil, &errCreateObject{playground.ErrNilPlayground}
	}
	if err := cxt.Err(); err != nil {
		return nil, &errCreateObject{err}
	}

	var (
		dir  = playground.RandomDirection()
		dots playground.DotList
		err  error
	)

	switch dir {
	case playground.DIR_NORTH, playground.DIR_SOUTH:
		dots, err = pg.GetEmptyField(1, uint8(_SNAKE_START_LENGTH))
	case playground.DIR_EAST, playground.DIR_WEST:
		dots, err = pg.GetEmptyField(uint8(_SNAKE_START_LENGTH), 1)
	}

	if err != nil {
		return nil, &errCreateObject{err}
	}

	if dir == playground.DIR_SOUTH || dir == playground.DIR_EAST {
		dots = dots.Reverse()
	}

	// Parent context stores in snake to pass it to corpse when snake
	// will be died. Snakes context are passed in run func
	scxt, cncl := context.WithCancel(cxt)

	snake := &Snake{pg, dots, _SNAKE_START_LENGTH, dir, time.Now(),
		cxt, cncl}

	if err = pg.Locate(snake); err != nil {
		return nil, &errCreateObject{err}
	}

	if err := snake.run(scxt); err != nil {
		pg.Delete(snake)
		return nil, &errCreateObject{err}
	}

	return snake, nil
}
示例#4
0
func CreateWall(pg *playground.Playground, dots playground.DotList,
) (Wall, error) {
	if pg == nil {
		return nil, &errCreateObject{playground.ErrNilPlayground}
	}
	if len(dots) == 0 {
		return nil, &errCreateObject{playground.ErrEmptyDotList}
	}

	var w Wall = Wall(dots)

	if err := pg.Locate(w); err != nil {
		return nil, &errCreateObject{err}
	}

	return w, nil
}
示例#5
0
// CreateApple creates and locates new apple
func CreateApple(pg *playground.Playground) (*Apple, error) {
	if pg == nil {
		return nil, &errCreateObject{playground.ErrNilPlayground}
	}

	dot, err := pg.GetEmptyDot()
	if err != nil {
		return nil, &errCreateObject{err}
	}

	apple := &Apple{pg, dot}

	if err := pg.Locate(apple); err != nil {
		return nil, &errCreateObject{err}

	}

	return apple, nil
}