Exemple #1
0
func (g *Game) destroyItem(json consts.JsonType) consts.JsonType {
	res := utils.JsonAction("destroyItem", "badId")
	id, ok := utils.GetIdFromJson(json)
	if ok {
		item := g.items.getItem(id)
		p := g.players.getPlayerBySession(json["sid"].(string))
		if item != nil && (item.IsOwner(p) || (!item.HasOwner() && geometry.Distance(p.GetCenter(), item.GetCenter()) <= consts.PICK_UP_RADIUS)) {
			var amount int = 1
			if json["amount"] != nil {
				amount = int(json["amount"].(float64))
			}
			// if item.GetAmount() - amount <= 0 {
			g.items.deleteItem(item)
			if item.IsOwner(p) {
				_, new_item := p.DeleteItem(item, amount)
				if new_item != nil {
					g.items.addItem(new_item)
				}
			} else if !item.HasOwner() {
				g.field.UnlinkFromCells(item)
			}
			// } else {
			//     item.DecAmount(amount)
			// }
			res["result"] = "ok"
		}
	}
	return res
}
Exemple #2
0
func (p *Player) Attack() consts.JsonType {
	var res consts.JsonType = nil
	li := p.slots[consts.SLOT_LEFT_HAND].item
	ri := p.slots[consts.SLOT_RIGHT_HAND].item
	ai := p.slots[consts.SLOT_AMMO].item
	pc := p.GetCenter()
	target, existTarget := p.GetTarget()
	if ((li != nil && li.GetItemSubtype() == consts.ITEM_ST_BOW) || (ri != nil && ri.GetItemSubtype() == consts.ITEM_ST_BOW)) && ai != nil {
		var pt geometry.Point
		if existTarget {
			pt = target.GetCenter()
		} else {
			pt = *p.GetAttackPoint()
		}
		pm.PManager.NewArrowProjectile(&pc, &pt, 30, p)
		p.DeleteItem(ai, 1)
		// ai.DecAmount()
	} else if existTarget && (target.GetID() != p.GetID()) {
		if d := geometry.Distance(pc, target.GetCenter()); d <= p.GetAttackRadius() {
			res = target.GetHit(p.weapon, p)
		}
	}
	p.ClearAttackPoint()
	p.Target = nil
	return res
}
Exemple #3
0
func (g *Game) pickUpItem(json consts.JsonType) consts.JsonType {
	res := utils.JsonAction("pickUp", "badId")
	id, ok := utils.GetIdFromJson(json)
	if ok {
		item := g.items.getItem(id)
		p := g.players.getPlayerBySession(json["sid"].(string))
		if *consts.FEATURE {
			if p.Killed() {
				return utils.JsonAction(res["action"].(string), "killed")
			}
		}
		if item != nil && !item.HasOwner() && geometry.Distance(p.GetCenter(), item.GetCenter()) <= float64(consts.PICK_UP_RADIUS) {
			if p.CanPickUp(item) {
				ok, i := p.PickUpItem(item)
				if ok {
					g.field.UnlinkFromCells(item)
					if item.GetID() != i.GetID() {
						g.items.deleteItem(item)
						item = nil
					}
					res["result"] = "ok"
				}
			} else {
				res["result"] = "tooHeavy"
			}
		}
	}
	return res
}
Exemple #4
0
func (m *Mob) Attack() consts.JsonType {
	var res consts.JsonType = nil
	if target, exists := m.GetTarget(); exists && target.GetID() != m.GetID() {
		m.ClearAttackPoint()
		bl := m.Kind.(*MobKind).blowList
		t, _ := m.GetTarget()
		if d := geometry.Distance(m.GetCenter(), t.GetCenter()); d <= m.GetAttackRadius() {
			// fmt.Printf("dist %f, attack radius %f\n", geometry.Distance(m.GetCenter(), t.GetCenter()), m.GetAttackRadius())
			// fmt.Printf("mob %d attack obj race - %d, id - %d\n", m.GetID(), t.GetKind().GetRace(), t.GetID())
			res = t.GetHit(bl.ChooseBlowMethod(consts.BT_MELEE), m)
		}
	}
	return res
}
func (pm *ProjectileManager) Do() {
	for _, p := range pm.projectiles {
		pm.field.UnlinkFromCells(p)
		var shift float64
		d := geometry.Distance(p.GetCenter(), p.GetDestination())
		if d > consts.PROJECTILE_VELOCITY {
			shift = consts.PROJECTILE_VELOCITY
		} else {
			shift = d
		}
		x := p.GetDestination().X - p.GetCenter().X
		y := p.GetDestination().Y - p.GetCenter().Y
		norm := math.Sqrt(x*x + y*y)
		dx, dy := x*shift/norm, y*shift/norm
		if collisionOccured, actor := pm.CheckCollision(p, dx, dy); collisionOccured || (x == 0 && y == 0) {
			if fb, ok := p.(*pM.AreaDamageProjectile); ok {
				x, y := fb.GetCenter().X, fb.GetCenter().Y
				notifier.GameNotifier.NotifyAboutFireball(x, y, fb.Radius)
				lt, rb := pm.field.GetSquareArea(x, y, fb.Radius)
				l, r := int(lt.X), int(rb.X)
				t, b := int(lt.Y), int(rb.Y)
				notified := make(map[int64]bool)
				for i := t; i < b; i++ {
					for j := l; j < r; j++ {
						for _, actor := range pm.field.GetActors(j, i) {
							id := actor.GetID()
							if !notified[id] {
								notified[id] = true
								go notifier.GameNotifier.NotifyAboutAttack(p.GetOwner(), actor, actor.GetHit(p, p.GetOwner()))
							}
						}
					}
				}
			} else {
				if actor != nil {
					go notifier.GameNotifier.NotifyAboutAttack(p.GetOwner(), actor, actor.GetHit(p, p.GetOwner()))
				}
			}
			delete(pm.projectiles, p.GetID())
		} else {
			p.Shift(dx, dy)
			pm.field.LinkToCells(p)
		}
	}
}