#region Copyright & License Information /* * Copyright 2007-2010 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, * see LICENSE. */ #endregion using System.Collections.Generic; using System.Linq; using OpenRA.GameRules; using OpenRA.Traits; namespace OpenRA.Orders { public class PlaceBuildingOrderGenerator : IOrderGenerator { readonly Actor Producer; readonly string Building; BuildingInfo BuildingInfo { get { return Rules.Info[ Building ].Traits.Get(); } } public PlaceBuildingOrderGenerator(Actor producer, string name) { Producer = producer; Building = name; } public IEnumerable Order(World world, int2 xy, MouseInput mi) { if (mi.Button == MouseButton.Right) world.CancelInputMode(); return InnerOrder(world, xy, mi); } IEnumerable InnerOrder(World world, int2 xy, MouseInput mi) { if (mi.Button == MouseButton.Left) { var topLeft = xy - Footprint.AdjustForBuildingSize( BuildingInfo ); if (!world.CanPlaceBuilding( Building, BuildingInfo, topLeft, null) || !world.IsCloseEnoughToBase(Producer.Owner, Building, BuildingInfo, topLeft)) { var eva = world.WorldActor.Info.Traits.Get(); Sound.Play(eva.BuildingCannotPlaceAudio); yield break; } if (Rules.Info[ Building ].Traits.Contains()) yield return new Order("LineBuild", Producer.Owner.PlayerActor, topLeft, Building); else yield return new Order("PlaceBuilding", Producer.Owner.PlayerActor, topLeft, Building); } } public void Tick( World world ) { // Find the queue with the target actor var queue = Producer.TraitsImplementing() .Where(p => p.CurrentItem() != null && p.CurrentItem().Item == Building && p.CurrentItem().RemainingTime == 0) .FirstOrDefault(); if (queue == null) world.CancelInputMode(); } public void RenderAfterWorld( World world ) {} public void RenderBeforeWorld(World world) { world.WorldRenderer.uiOverlay.DrawBuildingGrid(world, Building, BuildingInfo); } public string GetCursor(World world, int2 xy, MouseInput mi) { return "default"; } } }