1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
| Graphics = "datafiles/3D/";
local player;
local myRoot;
local passing;
local point_p1;
local point_p2; -- komi
local ending = true;
players = { LD.SYSTEM.Player:new(), LD.SYSTEM.Player:new() };
lastPlay = {nil, nil};
function main()
mw = LD.GUI.MainWindow:new("[Prototype]Last Dungeon, the best game ever !", 700, 700, true);
LD.GUI.Gui:setHandler("OnEvent");
device = mw:device();
createBackground();
color = LD.GUI.Color:new(0,0,255);
mw:start("pvp", color);
end
function createBackground()
bpvp = LD.GUI.Background:new("pvp");
bpvp:setCallbackFunction("OnEventPvP");
bpvp:setPrimaryCallbackFunction("OnPrimaryEventPvP");
bquit = LD.GUI.Background:new("quit");
bquit:setCallbackFunction("OnEventQuit");
end
function OnEventQuit(args)
standEvent = tolua.cast(args,"const LD::SYSTEM::StandardEvent");
if standEvent.name ~= "quit" then
LD.GUI.MainWindow:instance():changeBackground("");
end
end
function OnEventPvP(args)
standEvent = tolua.cast(args,"const LD::SYSTEM::StandardEvent");
if standEvent.name == "enter" then
loadMap(9);
loadGui();
init();
end
if standEvent.name == "restart" then
init();
end
if ending then
return;
end
if standEvent.name == "pass" then
pass();
end
if standEvent.name == "surrender" then
surrender();
end
end
function OnPrimaryEventPvP(args)
event = tolua.cast(args,"const LD::SYSTEM::Event");
if event:type() == "keyboard" then
keyevent = tolua.cast(args,"const LD::SYSTEM::KeyboardEvent");
if keyevent.keydown ~= true then
-- p is for pass turn
if keyevent.key == 80 then
sendEvent("pass");
return true;
end
-- a is for surrender
if keyevent.key == 65 then
sendEvent("surrender");
return true;
end
-- r is for restart
if keyevent.key == 82 then
sendEvent("restart");
return true;
end
end
end
if event:type() == "mouse" then
mouseevent = tolua.cast(args,"const LD::SYSTEM::MouseEvent");
if mouseevent:isLeft() and mouseevent:isDoubleClic() then
x = mouseevent.x;
y = mouseevent.y;
element = LD.I3D.Element3D:getElementAtPosition(x,y);
if element and element:type() == "plinth" then
plinth = LD.I3D.Plinth:toPlinth(element);
element:type();
element:type();
playOnPlinth(plinth);
end
end
end
return false;
end
function sendEvent(event)
back = LD.GUI.MainWindow:instance():background();
standevent = LD.SYSTEM.StandardEvent:new(back, event);
back:postEvent(standevent);
end
function loadMap(size)
ymax = 27 + 34;
yoffset = 27;
y = yoffset - (ymax - size*5)/2;
xmax= 27 + 34;
xoffset = 27;
x = (xmax - size*5)/2 - xoffset;
-- droite, haut, fond
position = LD.I3D.V3Df(x,y,40);
angle = LD.I3D.V3Df(270,0,270);
socles = std.vector_LD__I3D__Plinth__:new();
socles:resize(size*size);
num = 0;
for i = 1, size, 1 do
position.y = y;
for j = 1, size, 1 do
socle = createPlinth(position, angle);
socles[num] = socle;
num = num + 1;
addNeightBourg( socles, socle, i, j, size);
position.y = position.y - 5;
end
position.x = position.x + 5
end
position:delete();
angle:delete();
end
function createPlinth(position, angle)
pl = LD.I3D.Plinth:new( Graphics .. "plinth.3ds", false);
pl:setPosition( position );
pl:setRotation( angle );
return pl;
end
function addNeightBourg(socles, socle, i, j, size)
if i ~= 1 then
ti = i - 2;
tj = j - 1;
socle:addNeightBourg(socles[ti*size + tj]);
end
if j ~= 1 then
ti = i - 1;
tj = j - 2;
socle:addNeightBourg(socles[ti*size + tj]);
end
end
function loadGui()
local schememan = CEGUI.SchemeManager:getSingleton();
local fontman = CEGUI.FontManager:getSingleton();
local wmgr = CEGUI.WindowManager:getSingleton();
schememan:create( "TaharezLook.scheme" );
fontman:create( "DejaVuSans-10.font" );
fontman:create( "Commonwealth-10.font" );
myRoot = wmgr:createWindow( "TaharezLook/FrameWindow", "root" );
CEGUI.System:getSingleton():setGUISheet( myRoot );
myRoot:setPosition( CEGUI.UVector2(CEGUI.UDim(0.2,0),CEGUI.UDim(0,-10)));
myRoot:setSize( CEGUI.UVector2(CEGUI.UDim(0.6,0),CEGUI.UDim(0,20)));
end
function getNextPlayer()
passing = false;
if player == 1 then
player = 2;
else
player = 1;
end
setText("Player", "Player " .. player.. ", it's your turn");
end
do
Score = "";
Action = "";
Player = "";
function setText(type, text)
if type == "Score" then
Score = text;
end
if type == "Action" then
Action = text;
end
if type == "Player" then
Player = text;
end
myRoot:setText(Score .. " " .. Action .. " " .. Player);
end
end
function pass()
lastPlay[player] = nil;
setText("Action", "Player " .. player .. "pass.");
if passing then
removeUselessPawn();
endOfParty();
else
getNextPlayer();
passing = true;
end
end
function endOfParty()
winner = 1;
if point_p2 > point_p1 then
winner = 2;
end
setText("Player", "Player " .. winner .. " win.");
end
function removeUselessPawn()
-- todo
end
function init()
LD.I3D.PawnGroup:removeAll();
player = 0;
getNextPlayer();
point_p1 = 0;
point_p2 = 7.5;
passing = false;
ending = false;
setText("Score", point_p1 .. ":" .. point_p2);
setText("Action", "Game start");
end
function surrender()
if player == 2 then
point_p2 = 0;
else
point_p1 = 0;
end
setText("Action", "Player " .. player .. " surrender.");
endOfParty();
end
function OnEvent(args)
event = tolua.cast(args,"const LD::SYSTEM::Event");
back = LD.GUI.MainWindow:instance():background();
return back:onEvent(event);
end
function playOnPlinth(plinth)
-- droit de poser ?
if not plinth:isEmpty() then
return;
end
if lastPlay[player] ~= nil and lastPlay[player] == plinth then
return;
end
neights = plinth:neigthbourg();
ok = false
table = {}
for i=0,neights:size()-1,1 do
if neights[i]:isEmpty() then
ok = true
break;
else
if neights[i]:pawn():player() == players[player] then --même joueur
if table[neights[i]:pawn():group()] == nil then
if neights[i]:pawn():group():freedom() > neights:size() - i then
ok = true;
break;
else
table[neights[i]:pawn():group()] = neights[i]:pawn():group():freedom() - 1;
end
else
if table[neights[i]:pawn():group()] > neights:size() - i then
ok = true;
break;
else
table[neights[i]:pawn():group()] = table[neights[i]:pawn():group()] - 1;
end
end
else -- adversaire
if table[neights[i]:pawn():group()] == nil then
if neights[i]:pawn():group():freedom() <= 1 then
ok = true;
break;
else
table[neights[i]:pawn():group()] = neights[i]:pawn():group():freedom() - 1;
end
else
if table[neights[i]:pawn():group()] <= 1 then
ok = true;
break;
else
table[neights[i]:pawn():group()] = table[neights[i]:pawn():group()] - 1;
end
end
end
end
end
if not ok then
return
end
-- pose
pawn = putPawn(plinth, player);
-- add to groupe
size = neights:size();
for i=0,size-1,1 do
if neights[i]:pawn() then
pGroup = neights[i]:pawn():group():firstPlayer();
pPawn = pawn:group():firstPlayer();
if pGroup == pPawn then
pawn:group():fusion( neights[i]:pawn():group() );
else
--destroy other group
if neights[i]:pawn():group():freedom() <= 0 then
point = pawn:group():eatGroup( neights[i]:pawn():group() );
if player == 1 then
point_p1 = point_p1 + point;
else
point_p2 = point_p2 + point;
end
end
end
end
end
-- mise à jour points
setText("Score", point_p1 .. ":" .. point_p2);
setText("Action", "Player " .. player .. " put a pawn.");
getNextPlayer();
end
function putPawn(plinth, player)
pawn = LD.I3D.Pawn:new(players[player], Graphics .. "pawn" .. player .. ".3ds");
pawn:putOnPlinth(plinth);
pawn:setParentE(plinth);
lastPlay[player] = plinth;
return pawn;
end
main() |
Partager