示例#1
0
文件: base.go 项目: ZurgInq/protogame
func MoveToPlayer(room *g.Room, location *g.Location, monster *g.Monster, player *g.Player) (description []string) {
	if monster.VisibleRange == 0 || !player.InRadius(monster.PosObj, monster.VisibleRange) {
		return description
	}

	direct := movement.DetectDirectTo(monster.PosObj, player.PosObj)

	return movement.MoveMonster(room, location, monster, direct)
}
示例#2
0
func PlayerAttackToMonster(player *g.Player, monster *g.Monster) (battleLog []string) {
	battleLog = make([]string, 0)
	battleLog = append(battleLog, "***")
	if !monster.IsLive() || !player.IsLive() {
		return battleLog
	}
	if player.MP-player.CurrentWeapon().ManaCost < 0 {
		battleLog = append(battleLog, fmt.Sprintln("Недостаточно маны"))
		return
	}
	player.MP -= player.CurrentWeapon().ManaCost
	battleLog = append(battleLog, fmt.Sprintln("Вы атаковали:", monster.Name(), "("+strconv.Itoa(monster.HP)+")"))
	attacker := Attack{player}

	if attacker.Miss(monster) {
		battleLog = append(battleLog, fmt.Sprintln("Промах!"))
		return
	}

	damage, resultDamage := attacker.AttackTo(monster)

	battleLog = append(battleLog, fmt.Sprintln("Удар по противнику:", strconv.Itoa(damage)))
	battleLog = append(battleLog, fmt.Sprintln("Противник теряет", strconv.Itoa(resultDamage), "жизней"))

	if !monster.IsLive() {
		battleLog = append(battleLog, fmt.Sprintln("Противник пал"))
	}

	return battleLog
}
示例#3
0
func MonsterAttackToPlayer(monster *g.Monster, player *g.Player) (battleLog []string) {
	battleLog = make([]string, 0)
	battleLog = append(battleLog, "***")
	if !monster.IsLive() || !player.IsLive() {
		return
	}
	attacker := Attack{monster}

	if attacker.Miss(player) {
		battleLog = append(battleLog, fmt.Sprintln("Промах!"))
		return
	}
	damage, resultDamage := attacker.AttackTo(player)
	battleLog = append(battleLog, fmt.Sprintln(monster.Name(), "атаковал:", strconv.Itoa(damage)))
	battleLog = append(battleLog, fmt.Sprintln("Вы понесли урон:", strconv.Itoa(resultDamage)))

	return
}
示例#4
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
}