Пример #1
0
func (gen RoomsGenerator) markLocationAsFinish(room *g.Room, location *g.Location) {
	cellsCount := len(location.CellsMap)
	idx := cellsCount / 2
	location.CellsMap[idx] = "&"
	triggerX, triggerY := g.CellIdxToXY(idx, location.SizeX)
	exitTrigger := g.CreateExitTrigger(*g.NewPosObj(location, triggerX, triggerY))
	room.AddTrigger(exitTrigger)
	room.ExitLocationID = location.RefID()
}
Пример #2
0
func MovePlayer(room *g.Room, location *g.Location, player *g.Player, direct g.ActionDirect) (description []string) {
	description = make([]string, 0)
	if !player.IsLive() {
		description = append(description, "player is dead")
		return
	}

	var lastTime time.Time
	lastTime, ok := lastTimeAction[player.RefID()]
	if ok {
		if time.Now().Before(lastTime.Add(moveSleep)) {
			description = append(description, "wait")
			return
		}
	}
	lastTime = time.Now()
	lastTimeAction[player.RefID()] = lastTime

	oldPos, newPos := player.PosObj, g.PosObj{LocationID: location.RefID(), PosX: player.PosX, PosY: player.PosY}

	switch direct {
	case g.DirectMoveUpLeft:
		newPos.PosX--
		newPos.PosY--
	case g.DirectMoveUp:
		newPos.PosY--
	case g.DirectMoveUpRight:
		newPos.PosX++
		newPos.PosY--
	case g.DirectMoveDownLeft:
		newPos.PosX--
		newPos.PosY++
	case g.DirectMoveDown:
		newPos.PosY++
	case g.DirectMoveDownRight:
		newPos.PosX++
		newPos.PosY++
	case g.DirectMoveLeft:
		newPos.PosX--
	case g.DirectMoveRight:
		newPos.PosX++
	default:
		panic("unknown direct: " + direct)
	}

	player.IncSteps()

	for _, monster := range room.Monsters() {
		if monster.IsLive() && newPos.PosEqual(monster.PosObj) {
			description = b.PlayerAttackToMonster(player, monster)
			if monster.IsDead() && monster.Drops != nil {
				room.AddItem(
					&g.PlaceItem{
						PosObj: monster.PosObj,
						Item:   monster.Drops,
					},
				)
			}

			return description
		}
	}

	for _, trigger := range room.Triggers() {
		//into trigger
		if newPos.PosEqual(trigger.PosObj) {
			//trigger.ExecInto
			trigger.Exec(player, direct)
		}
		//out trigger
		if !oldPos.PosEqual(player.PosObj) && oldPos.PosEqual(trigger.PosObj) {
			//trigger.ExecOut
			trigger.Exec(player, direct)
		}
		//inside
		if player.PosObj.PosEqual(trigger.PosObj) {
			//trigger.ExecInside
			trigger.Exec(player, direct)
		}
	}

	if player.CanMove() && isMovable(location, newPos.PosX, newPos.PosY) {
		player.PosObj = newPos
		return
	}

	if !player.IsLive() {
		return append(description, "you are dead")
	}

	return
}