Commit fe8dfd90 authored by Craig Everett's avatar Craig Everett

Basic (unsafe) movement

parent 589d1c1f
[Dolphin]
PreviewsShown=true
Timestamp=2020,12,11,17,6,49
Version=4
[Dolphin]
PreviewsShown=true
Timestamp=2020,12,11,17,20,50
Version=4
......@@ -14,7 +14,7 @@
% UI control
-export([new_game/0]).
% Game control
-export([rotate/1]).
-export([random_piece/0, rotate/1, move/1]).
% System control
-export([start_link/0, stop/0]).
% gen_server
......@@ -45,12 +45,23 @@ new_game() ->
%%% Game control
% TODO: Remove this debugging function
random_piece() ->
gen_server:cast(?MODULE, random_piece).
-spec rotate(Direction) -> ok
when Direction :: l | r.
rotate(Direction) ->
gen_server:cast(?MODULE, {rotate, Direction}).
-spec move(Direction) -> ok
when Direction :: l | r | d.
move(Direction) ->
gen_server:cast(?MODULE, {move, Direction}).
%%% System control
......@@ -83,20 +94,10 @@ init(none) ->
ok = log(info, "Starting"),
Window = et_gui:start_link("Erltris"),
ok = log(info, "Window: ~p", [Window]),
State = #s{well = Well} = #s{window = Window},
Piece = et_piece:rand(),
NewWell = place(Piece, Well),
ok = et_gui:show(NewWell),
{ok, State#s{well = NewWell}}.
State = #s{window = Window},
{ok, State}.
place(Piece, Well) ->
PosX = 5,
PosY = 5,
Points = et_piece:points(Piece),
Type = et_piece:type(Piece),
Update = fun({X, Y}, Well) -> et_well:store(Well, Type, PosX + X, PosY + Y) end,
lists:foldl(Update, Well, Points).
%%% gen_server Message Handling Callbacks
......@@ -131,9 +132,15 @@ handle_call(Unexpected, From, State) ->
handle_cast(new_game, State) ->
NewState = do_new_game(State),
{noreply, NewState};
handle_cast(random_piece, State) ->
NewState = do_random_piece(State),
{noreply, NewState};
handle_cast({rotate, Direction}, State) ->
NewState = do_rotate(Direction, State),
{noreply, NewState};
handle_cast({move, Direction}, State) ->
NewState = do_move(Direction, State),
{noreply, NewState};
handle_cast(stop, State) ->
ok = log(info, "Received a 'stop' message."),
% {noreply, State};
......@@ -177,6 +184,34 @@ do_new_game(State) ->
State.
do_rotate(_, State) ->
ok = tell(info, "do_rotate/2 not yet implemented."),
State.
do_random_piece(State) ->
Piece = et_piece:rand(),
Loc = {5, 5},
NewWell = place(Piece, Loc, et_well:new()),
ok = et_gui:show(NewWell),
State#s{well = NewWell, loc = Loc, piece = Piece}.
place(Piece, {PosX, PosY}, Well) ->
Points = et_piece:points(Piece),
Type = et_piece:type(Piece),
Update = fun({X, Y}, W) -> et_well:store(W, Type, PosX + X, PosY + Y) end,
lists:foldl(Update, Well, Points).
do_rotate(Direction, State = #s{loc = Loc, piece = Piece}) ->
NewPiece = et_piece:flip(Direction, Piece),
NewWell = place(NewPiece, Loc, et_well:new()),
ok = et_gui:show(NewWell),
State#s{well = NewWell, piece = NewPiece}.
do_move(Direction, State = #s{loc = {X, Y}, piece = Piece}) ->
Loc =
case Direction of
r -> {X + 1, Y};
l -> {X - 1, Y};
d -> {X, Y + 1}
end,
NewWell = place(Piece, Loc, et_well:new()),
ok = et_gui:show(NewWell),
State#s{well = NewWell, loc = Loc}.
......@@ -111,7 +111,7 @@ init(Label) ->
ok = wxFrame:connect(Frame, close_window),
ok = wxPanel:connect(Frame, char_hook),
ok = wxPanel:connect(Board, paint),
ok = wxFrame:setClientSize(Frame, {(W * 30) + 300, H * 30}),
ok = wxFrame:setClientSize(Frame, {(W * 30) + 150, H * 30}),
ok = wxFrame:center(Frame),
true = wxFrame:show(Frame),
State = #s{frame = Frame, board = Board, well = Well,
......@@ -188,6 +188,22 @@ handle_event(#wx{event = #wxClose{}}, State = #s{frame = Frame}) ->
ok = et_con:stop(),
ok = wxWindow:destroy(Frame),
{noreply, State};
handle_event(#wx{event = #wxKey{keyCode = Code}}, State = #s{frame = Frame}) ->
ok =
case Code of
32 -> et_con:random_piece();
88 -> et_con:rotate(l);
90 -> et_con:rotate(r);
314 -> et_con:move(l);
315 -> et_con:rotate(r);
316 -> et_con:move(r);
317 -> et_con:move(d);
_ -> tell("KeyCode: ~p", [Code])
end,
{noreply, State};
handle_event(#wx{event = #wxPaint{}}, State) ->
ok = redraw(State),
{noreply, State};
handle_event(Event, State) ->
ok = tell(info, "Unexpected event ~tp State: ~tp~n", [Event, State]),
{noreply, State}.
......@@ -213,23 +229,20 @@ do_show(Well, State) ->
redraw(#s{board = Board, well = Well, sprites = Sprites}) ->
{W, H} = et_well:dimensions(Well),
DC = wxClientDC:new(Board),
tell("Got my new DC: ~p", [DC]),
ok = draw(1, 1, W, H, DC, Well, Sprites),
wxClientDC:destroy(DC).
draw(W, H, W, H, DC, Well, Sprites) ->
T = et_well:fetch(Well, W, H),
paint(W, H, DC, T, Sprites);
paint(W, H, DC, Well, Sprites);
draw(W, Y, W, H, DC, Well, Sprites) ->
T = et_well:fetch(Well, W, Y),
ok = paint(W, Y, DC, T, Sprites),
ok = paint(W, Y, DC, Well, Sprites),
draw(1, Y + 1, W, H, DC, Well, Sprites);
draw(X, Y, W, H, DC, Well, Sprites) ->
T = et_well:fetch(Well, X, Y),
ok = paint(X, Y, DC, T, Sprites),
ok = paint(X, Y, DC, Well, Sprites),
draw(X + 1, Y, W, H, DC, Well, Sprites).
paint(X, Y, DC, T, Sprites) ->
paint(X, Y, DC, Well, Sprites) ->
T = et_well:fetch(Well, X, Y),
Image = maps:get(T, Sprites),
Coord = {(X - 1) * 30, (Y - 1) * 30},
wxClientDC:drawBitmap(DC, Image, Coord).
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment