• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang ecs.Entity类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/paked/engi/ecs.Entity的典型用法代码示例。如果您正苦于以下问题:Golang Entity类的具体用法?Golang Entity怎么用?Golang Entity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Entity类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: Update

func (bs *BallSystem) Update(entity *ecs.Entity, dt float32) {
	var space *engi.SpaceComponent
	var speed *SpeedComponent
	if !entity.Component(&space) || !entity.Component(&speed) {
		return
	}

	if space.Position.X < 0 {
		engi.Mailbox.Dispatch(ScoreMessage{1})

		space.Position.X = 400 - 16
		space.Position.Y = 400 - 16
		speed.X = 800 * rand.Float32()
		speed.Y = 800 * rand.Float32()
	}

	if space.Position.Y < 0 {
		space.Position.Y = 0
		speed.Y *= -1
	}

	if space.Position.X > (800 - 16) {
		engi.Mailbox.Dispatch(ScoreMessage{2})

		space.Position.X = 400 - 16
		space.Position.Y = 400 - 16
		speed.X = 800 * rand.Float32()
		speed.Y = 800 * rand.Float32()
	}

	if space.Position.Y > (800 - 16) {
		space.Position.Y = 800 - 16
		speed.Y *= -1
	}
}
开发者ID:EtienneBruines,项目名称:engi,代码行数:35,代码来源:headless.go


示例2: Update

func (fs *FallingSystem) Update(entity *ecs.Entity, dt float32) {
	var space *engi.SpaceComponent
	if !entity.Component(&space) {
		return
	}
	space.Position.Y += 200 * dt
}
开发者ID:EtienneBruines,项目名称:engi,代码行数:7,代码来源:falling.go


示例3: Update

func (c *HideSystem) Update(e *ecs.Entity, dt float32) {
	var render *engi.RenderComponent
	if !e.Component(&render) {
		return
	}
	if rand.Int()%10 == 0 {
		render.SetPriority(engi.Hidden)
	} else {
		render.SetPriority(engi.MiddleGround)
	}
}
开发者ID:EtienneBruines,项目名称:engi,代码行数:11,代码来源:hide.go


示例4: Update

func (c *ControlSystem) Update(entity *ecs.Entity, dt float32) {
	var mouse *engi.MouseComponent
	if !entity.Component(&mouse) {
		return
	}

	if mouse.Enter {
		engi.SetCursor(engi.Hand)
	} else if mouse.Leave {
		engi.SetCursor(engi.Arrow)
	}
}
开发者ID:EtienneBruines,项目名称:engi,代码行数:12,代码来源:mouse.go


示例5: Update

func (rs *RenderSystem) Update(entity *ecs.Entity, dt float32) {
	if !rs.changed {
		return
	}

	var render *RenderComponent
	var ok bool

	if render, ok = entity.ComponentFast(render).(*RenderComponent); !ok {
		return
	}

	rs.renders[render.priority] = append(rs.renders[render.priority], entity)
}
开发者ID:EtienneBruines,项目名称:engi,代码行数:14,代码来源:render.go


示例6: Update

func (c *ControlSystem) Update(entity *ecs.Entity, dt float32) {
	var a *engi.AnimationComponent

	if !entity.Component(&a) {
		return
	}

	if engi.Keys.Get(engi.ArrowRight).Down() {
		a.SelectAnimationByAction(WalkAction)
	} else if engi.Keys.Get(engi.Space).Down() {
		a.SelectAnimationByAction(SkillAction)
	} else {
		a.SelectAnimationByAction(StopAction)
	}
}
开发者ID:EtienneBruines,项目名称:engi,代码行数:15,代码来源:anim.go


示例7: Update

func (c *ScaleSystem) Update(e *ecs.Entity, dt float32) {
	var render *engi.RenderComponent
	if !e.Component(&render) {
		return
	}
	var mod float32

	if rand.Int()%2 == 0 {
		mod = 0.1
	} else {
		mod = -0.1
	}

	if render.Scale().X+mod >= 15 || render.Scale().X+mod <= 1 {
		mod *= -1
	}

	newScale := render.Scale()
	newScale.AddScalar(mod)
	render.SetScale(newScale)
}
开发者ID:EtienneBruines,项目名称:engi,代码行数:21,代码来源:scenes.go


示例8: Update

func (a *MovementSystem) Update(entity *ecs.Entity, dt float32) {
	var move *MovementComponent
	if !entity.Component(&move) {
		return
	}

	if !move.started {
		move.started = true
		move.timeLeft = float32(move.In.Seconds())
		move.speedX = (move.From.X - move.To.X) / move.timeLeft
		move.speedY = (move.From.Y - move.To.Y) / move.timeLeft
	}
	move.timeLeft -= dt

	var space *engi.SpaceComponent
	if !entity.Component(&space) {
		return
	}

	space.Position.X -= move.speedX * dt
	space.Position.Y -= move.speedY * dt

	if move.timeLeft < 0 {
		// Because we might move more than needed
		space.Position.X -= move.speedX * move.timeLeft
		space.Position.Y -= move.speedY * move.timeLeft
		move.timeLeft = 0
	}

	if move.timeLeft == 0 {
		entity.RemoveComponent(move)
		move.Callback()
	}
}
开发者ID:EtienneBruines,项目名称:bcigame,代码行数:34,代码来源:move.go


示例9: Update

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)
}
开发者ID:EtienneBruines,项目名称:bcigame,代码行数:39,代码来源:calibrate.go


示例10: Update

func (cs *CollisionSystem) Update(entity *ecs.Entity, dt float32) {
	var (
		space     *SpaceComponent
		collision *CollisionComponent
		ok        bool
	)

	if space, ok = entity.ComponentFast(space).(*SpaceComponent); !ok {
		return
	}

	if collision, ok = entity.ComponentFast(collision).(*CollisionComponent); !ok {
		return
	}

	if !collision.Main {
		return
	}

	var (
		otherSpace     *SpaceComponent
		otherCollision *CollisionComponent
	)

	for _, other := range cs.Entities() {
		if other.ID() != entity.ID() {
			if otherSpace, ok = other.ComponentFast(otherSpace).(*SpaceComponent); !ok {
				return
			}

			if otherCollision, ok = other.ComponentFast(otherCollision).(*CollisionComponent); !ok {
				return
			}

			entityAABB := space.AABB()
			offset := Point{collision.Extra.X / 2, collision.Extra.Y / 2}
			entityAABB.Min.X -= offset.X
			entityAABB.Min.Y -= offset.Y
			entityAABB.Max.X += offset.X
			entityAABB.Max.Y += offset.Y
			otherAABB := otherSpace.AABB()
			offset = Point{otherCollision.Extra.X / 2, otherCollision.Extra.Y / 2}
			otherAABB.Min.X -= offset.X
			otherAABB.Min.Y -= offset.Y
			otherAABB.Max.X += offset.X
			otherAABB.Max.Y += offset.Y
			if IsIntersecting(entityAABB, otherAABB) {
				if otherCollision.Solid && collision.Solid {
					mtd := MinimumTranslation(entityAABB, otherAABB)
					space.Position.X += mtd.X
					space.Position.Y += mtd.Y
				}

				Mailbox.Dispatch(CollisionMessage{Entity: entity, To: other})
			}
		}
	}
}
开发者ID:EtienneBruines,项目名称:engi,代码行数:58,代码来源:collision.go


示例11: Update

func (as *AudioSystem) Update(entity *ecs.Entity, dt float32) {
	var ac *AudioComponent
	var ok bool
	if ac, ok = entity.ComponentFast(ac).(*AudioComponent); !ok {
		return
	}

	if ac.player == nil {
		f := Files.Sound(ac.File)
		if f == nil {
			return
		}

		var err error
		ac.player, err = NewPlayer(f, 0, 0)
		if err != nil {
			log.Println("Error initializing AudioSystem:", err)
			return
		}
	}

	if ac.player.State() != Playing {
		if ac.player.State() == Stopped {
			if !ac.Repeat {
				al.RewindSources(ac.player.source)
				al.StopSources(ac.player.source)
				entity.RemoveComponent(ac)
				return
			}
		}

		// Prepares if the track hasn't been buffered before.
		if err := ac.player.prepare(ac.Background, 0, false); err != nil {
			log.Println("Error initializing AudioSystem:", err)
			return
		}

		al.PlaySources(ac.player.source)

		if !ac.Background {
			var space *SpaceComponent
			var ok bool
			if space, ok = entity.ComponentFast(space).(*SpaceComponent); !ok {
				return
			}

			ac.player.source.SetPosition(al.Vector{
				(space.Position.X + space.Width/2) / Width(),
				(space.Position.Y + space.Height/2) / Height(),
				0})
		}
	}
}
开发者ID:EtienneBruines,项目名称:engi,代码行数:53,代码来源:audio.go


示例12: Update

// Update sets the MouseComponent values for each Entity
func (m *MouseSystem) Update(entity *ecs.Entity, dt float32) {
	var (
		mc     *MouseComponent
		space  *SpaceComponent
		render *RenderComponent
		ok     bool
	)

	// We need MouseComponent to save our findings
	if mc, ok = entity.ComponentFast(mc).(*MouseComponent); !ok {
		return
	}

	// We need SpaceComponent for the location
	if space, ok = entity.ComponentFast(space).(*SpaceComponent); !ok {
		return
	}

	// We need RenderComponent for the Priority
	if render, ok = entity.ComponentFast(render).(*RenderComponent); !ok {
		return
	}

	// Reset some values
	mc.Leave = false

	mx := m.mouseX
	my := m.mouseY

	// Special case: HUD
	if render.priority >= HUDGround {
		mx = Mouse.X
		my = Mouse.Y
	}

	// if the Mouse component is a tracker we always update it
	// Check if the X-value is within range
	// and if the Y-value is within range
	if mc.Track || mx > space.Position.X && mx < (space.Position.X+space.Width) &&
		my > space.Position.Y && my < (space.Position.Y+space.Height) {

		mc.Enter = !mc.Hovered
		mc.Hovered = true

		mc.Released = false
		// track mouse position so that systems that need to stay on the mouse
		// position can do it (think an RTS when placing a new building and
		// you get a ghost building following your mouse until you click to
		// place it somewhere in your world.
		mc.MouseX = mx
		mc.MouseY = my

		switch Mouse.Action {
		case PRESS:
			switch Mouse.Button {
			case MouseButtonLeft:
				mc.Clicked = true
			case MouseButtonRight:
				mc.RightClicked = true
			}
			m.mouseDown = true
		case RELEASE:
			switch Mouse.Button {
			case MouseButtonLeft:
				mc.Released = true
			case MouseButtonRight:
				mc.RightReleased = true
			}
			// dragging stops as soon as one of the currently pressed buttons
			// is released
			mc.Dragged = false
			// mouseDown goes false as soon as one of the pressed buttons is
			// released. Effectively ending any dragging
			m.mouseDown = false
		case MOVE:
			if m.mouseDown {
				mc.Dragged = true
			}
		}
	} else {
		if mc.Hovered {
			mc.Leave = true
		}
		mc.Hovered = false
	}
}
开发者ID:EtienneBruines,项目名称:engi,代码行数:87,代码来源:mouse.go


示例13: Update

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
				}
			}
		},
	})
}
开发者ID:EtienneBruines,项目名称:bcigame,代码行数:72,代码来源:maze.go



注:本文中的github.com/paked/engi/ecs.Entity类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang ecs.World类代码示例发布时间:2022-05-28
下一篇:
Golang ecs.NewSystem函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap