コード例 #1
0
ファイル: calibrate.go プロジェクト: EtienneBruines/bcigame
func (c *Calibrate) Update(entity *ecs.Entity, dt float32) {
	if c.frameIndex != 0 {
		return
	}

	var (
		cal *CalibrateComponent
		ok  bool
	)

	if cal, ok = entity.ComponentFast(cal).(*CalibrateComponent); !ok {
		return
	}

	// Render the image again
	plt, err := plot.New()
	if err != nil {
		log.Fatal(err)
	}

	plotutil.AddLinePoints(plt,
		"CH"+strconv.Itoa(int(cal.ChannelIndex)), plotter.XYer(c.channels[cal.ChannelIndex]))
	img := image.NewRGBA(image.Rect(0, 0, 3*dpi, 3*dpi))
	canv := vgimg.NewWith(vgimg.UseImage(img))
	plt.Draw(draw.New(canv))
	bgTexture := engi.NewImageRGBA(img)

	// Give it to engi

	erender := &engi.RenderComponent{
		Display:      engi.NewRegion(engi.NewTexture(bgTexture), 0, 0, 3*dpi, 3*dpi),
		Scale:        engi.Point{1, 1},
		Transparency: 1,
		Color:        color.RGBA{255, 255, 255, 255},
	}
	erender.SetPriority(engi.HUDGround)

	entity.AddComponent(erender)
}
コード例 #2
0
ファイル: maze.go プロジェクト: EtienneBruines/bcigame
func (m *Maze) Update(entity *ecs.Entity, dt float32) {
	if entity.ID() != m.playerEntity.ID() {
		return
	}

	var (
		move *MovementComponent
		ok   bool
	)

	if move, ok = entity.ComponentFast(move).(*MovementComponent); ok {
		return // because we're still moving!
	}

	if m.currentLevel.Width == 0 || m.currentLevel.Height == 0 {
		return // because there's no maze
	}

	oldX, oldY := m.currentLevel.PlayerX, m.currentLevel.PlayerY

	if m.currentLevel.Grid[oldY][oldX] == TileGoal {
		// Goal achieved!

		if strings.HasPrefix(m.currentLevel.Name, "Random ") {
			engi.Mailbox.Dispatch(MazeMessage{})
			return
		}

		if m.sequence != SequenceNone {
			m.cleanup()
			m.initialize("")
			return
		}
	}

	switch m.Controller.Action(m.currentLevel) {
	case ActionUp:
		m.currentLevel.PlayerY--
	case ActionDown:
		m.currentLevel.PlayerY++
	case ActionLeft:
		m.currentLevel.PlayerX--
	case ActionRight:
		m.currentLevel.PlayerX++
	case ActionStop:
		return // so don't move
	}

	if !m.currentLevel.IsAvailable(m.currentLevel.PlayerX, m.currentLevel.PlayerY) {
		m.currentLevel.PlayerX, m.currentLevel.PlayerY = oldX, oldY
		return // because it's an invalid move
	}

	entity.AddComponent(&MovementComponent{
		From: engi.Point{float32(oldX) * tileWidth, float32(oldY) * tileHeight},
		To:   engi.Point{float32(m.currentLevel.PlayerX) * tileWidth, float32(m.currentLevel.PlayerY) * tileHeight},
		In:   time.Second / moveSpeed,
		Callback: func() {
			if m.currentLevel.Grid[m.currentLevel.PlayerY][m.currentLevel.PlayerX] == TileRoute {
				m.currentLevel.Grid[m.currentLevel.PlayerY][m.currentLevel.PlayerX] = TileBlank
				m.currentLevel.GridEntities[m.currentLevel.PlayerY][m.currentLevel.PlayerX].AddComponent(tileBlank)
			} else if m.currentLevel.Grid[oldY][oldX] == TileError ||
				m.currentLevel.Grid[oldY][oldX] == TileBlank {
				m.currentLevel.Grid[oldY][oldX] = TileRoute

				if m.currentLevel.Grid[m.currentLevel.PlayerY][m.currentLevel.PlayerX] == TileHiddenError {
					m.currentLevel.Grid[m.currentLevel.PlayerY][m.currentLevel.PlayerX] = TileError
				}
			}
		},
	})
}