vtm/source/model/spot.ts
Christian Fraß 95ff0c95b3 restructuring
2018-03-29 01:13:39 +02:00

111 lines
2.9 KiB
TypeScript

/*
* Verrückte Turing-Maschinen — A turing complete game
* Copyright (C) 2016-2018 Christian Fraß <vidofnir@folksprak.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
module mod_vtm
{
export module mod_model
{
export module mod_spot
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
export type type_spot = {u : int; v : int;};
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function hash(spot : type_spot) : string
{
return (spot.u.toFixed(0) + "_" + spot.v.toFixed(0));
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function from_hash(hash : string) : type_spot
{
let teile : Array<string> = hash.split("_");
return {"u": parseInt(teile[0]), "v": parseInt(teile[1])};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function null_() : type_spot
{
return {"u": 0, "v": 0};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function from_direction(direction : mod_direction.type_direction) : type_spot
{
switch (direction)
{
case 0: {return {"u": +1, "v": 0}; break;}
case 1: {return {"u": +1, "v": +1}; break;}
case 2: {return {"u": 0, "v": +1}; break;}
case 3: {return {"u": -1, "v": 0}; break;}
case 4: {return {"u": -1, "v": -1}; break;}
case 5: {return {"u": 0, "v": -1}; break;}
default: {throw (new Error("ungültige Richtung '" + String(direction) + "'")); break;}
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function add(spot1 : type_spot, spot2 : type_spot) : type_spot
{
return {"u": (spot1.u + spot2.u), "v": (spot1.v + spot2.v)};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function export_(spot : type_spot) : string
{
return hash(spot);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function import_(spot_ : string) : type_spot
{
return from_hash(spot_);
}
}
}
}