Skip to content

Instantly share code, notes, and snippets.

@phrohdoh
phrohdoh / bool IsInRangeOf.cs
Created January 9, 2014 17:07
A function written before realizing OpenRA has FindActorsInCircle.
bool IsInRangeOf(Actor actorInQuestion, Actor sourceActor, bool use3D = false)
{
// convert distance (px -> cells)
var distX = (sourceActor.Location.CenterPosition.X - actorInQuestion.Location.CenterPosition.X) / 1024;
var distY = (sourceActor.Location.CenterPosition.Y - actorInQuestion.Location.CenterPosition.Y) / 1024;
var distZ = (sourceActor.Location.CenterPosition.Z - actorInQuestion.Location.CenterPosition.Z) / 1024;
// convert Info.Range (px -> cells)
var range = (Info.Range * 1024);
@phrohdoh
phrohdoh / BuildingUtils.Util.cs
Created May 20, 2014 23:23
Requesting help for stashing Plug/PlugActor in BuildingUtil/Util.cs ~11-12
public static bool CanPlaceBuilding(this World world, string name, BuildingInfo building, CPos topLeft, Actor toIgnore)
{
if (building.AllowInvalidPlacement)
return true;
var existing = world.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(topLeft);
var toPlace = Rules.Info[name.ToLowerInvariant()];
if (CanPlacePlug(existing, toPlace, topLeft))
{
// 1)
GetIndexer(this List<Form> list)
{
var ifi = new List<IFormIndexer>();
foreach (var item in list)
ifi.Add(item as IFormIndexer);
}
// or 2)
GetIndexer(this List<Form> list)
// sourcePngs is a ListBox with property Items (ListItemCollection)
// A custom list of strings in the end (custom to eto, not to me)
var pngs = sourcePngs.Items;
var temp = new string[pngs.Count];
for (var i = 0; i < temp.Length; i++)
temp[i] = pngs[i].Text;
var image = new Bitmap(100, 100, PixelFormat.Format24bppRgb);
using (var graphics = new Graphics(image))
{
graphics.DrawRectangle(Colors.Red, 15, 15, 50, 50);
}
using (var locked = image.Lock()) unsafe
{
int *value = (int*)locked.Data;
var image = new Bitmap(100, 100, PixelFormat.Format24bppRgb);
using (var locked = image.Lock()) unsafe
{
for (var x = 0; x < image.Width; x++)
for (var y = 0; y < image.Height; y++)
if (y == image.Height / 3)
image.SetPixel(x, y, Color.FromArgb(70, 0, 0));
}
var drawable = DrawableArea(image);
drawable.Invalidate();
Drawable DrawableArea(Bitmap image)
{
var ret = new Drawable();
ret.Paint += (sender, e) =>
{
e.Graphics.DrawImage(image, ret.Location.X, ret.Location.Y);
};
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace PrototypeRPG.Traits
{
public class World
{
public const int TickTimestep = 40;
// World.cs
public Rectangle GetSourceRectangle(Texture2D tileset, int tileIndex, int tileSize)
{
var borderSize = 1;
var paddedTileSize = tileSize + borderSize;
var tilesPerRow = tileset.Width / paddedTileSize;
var x = paddedTileSize * (tileIndex % tilesPerRow);
var y = paddedTileSize * (tileIndex / tilesPerRow);
return new Rectangle(x, y, tileSize, tileSize);
public void LoadTilesFromFile(string filename)
{
var fullText = File.ReadAllLines(filename);
var output = new int[fullText.Length][];
foreach (var fullLine in fullText)
{
var lineAsIntArray = fullLine.ToIntArray(',');
for (var intIndex = 0; intIndex < lineAsIntArray.Length; intIndex++)