-
Notifications
You must be signed in to change notification settings - Fork 3
/
throw-banana.sh
200 lines (171 loc) · 5.44 KB
/
throw-banana.sh
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
#!/bin/bash
# bash-gorillas is a demake of QBasic GORILLAS completely rewritten
# in Bash.
# Copyright (C) 2013 Istvan Szantai <szantaii at sidenote dot hu>
#
# 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 (LICENSE).
# If not, see <http://www.gnu.org/licenses/>.
# This fuction is responsible for banana throwing,
# including physics, animation, etc.
throw_banana()
{
# Necessary local variables
local pi=$(echo "scale=20; 4 * a(1)" | bc -l)
local x=""
local y=""
local x_0=""
local y_0=""
local prev_x=""
local prev_y=""
local throw_angle=""
local throw_speed=""
# Initialize banana animation frame
init_banana
# Set $throw_angle, $throw_speed, banana throw
# start positions, etc. based on read values and
# current player
if ((next_player == 1))
then
# Convert degrees to radians
throw_angle=$(echo "scale=20; ${player1_throw_angle} * ${pi} / 180" | \
bc -l)
# Set $throw_speed of player1
throw_speed=${player1_throw_speed}
# Set throw start coordinates of player1
x=${player1_throw_start_coordinates%","*}
y=${player1_throw_start_coordinates#*","}
x_0=${x}
y_0=${y}
# Start player1 throw animation
print_player1_throw_frame1
else
# Set correct angle for player2, and
# convert degrees to radians
throw_angle=$((180 - player2_throw_angle))
throw_angle=$(echo "scale=20; ${throw_angle} * ${pi} / 180" | \
bc -l)
# Set $throw_speed of player2
throw_speed=${player2_throw_speed}
# Set throw start coordinates of player2
x=${player2_throw_start_coordinates%","*}
y=${player2_throw_start_coordinates#*","}
x_0=${x}
y_0=${y}
# Start player2 throw animation
print_player2_throw_frame1
fi
# Print first banana frame to the screen
tput cup $(($((top_padding_height + grid_height)) - y)) \
$((left_padding_width + x)) >> ${buffer}
printf "${banana}" >> ${buffer}
refresh_screen
# Print player throw animation ending to the screen
# depending who is the current throwing player
if ((next_player == 1))
then
print_player1_throw_frame2
else
print_player2_throw_frame2
fi
# Banana throw loop:
#
for ((t=0; x >= 0 && x < grid_width && y >= 1 && y <= (grid_height * 5); ))
do
# Clear previous banana frame from screen,
# if there was a banana printed to the
# screen in the previous iteration
if [[ "${prev_x}" != "" && "${prev_y}" != "" ]] && \
((x >= 0 && x < grid_width && y >= 1 && y <= grid_height))
then
tput cup $(($((top_padding_height + grid_height)) - y)) \
$((left_padding_width + x)) >> ${buffer}
printf " " >> ${buffer}
refresh_screen
fi
# Calculate next horizontal ($x) and
# vertical ($y) position of the banana
x=$(echo "scale=20; ${x_0} + \
(${throw_speed} * ${t} * c(${throw_angle}) + \
(${wind_value} * ${t} * ${t}))" | bc -l | xargs printf "%1.0f\n")
y=$(echo "scale=20; ${y_0} + \
(${throw_speed} * ${t} * s(${throw_angle}) - \
(2 * ${gravity_value} / 2) * ${t} * ${t})" | \
bc -l | xargs printf "%1.0f\n")
# Collision detection START --------------------------------------------
# If the banana hits a building the building block
# will be erased and then comes the next player
if [[ "${grid["${x},$((y - 1))"]}" == "X" ]]
then
# Erase block from 'grid'
grid["${x},$((y - 1))"]=""
# Erase block from screen
tput cup $(($((top_padding_height + grid_height)) - y)) \
$((left_padding_width + x)) >> ${buffer}
printf " " >> ${buffer}
refresh_screen
# Exit the loop
break
fi
# Banana hits player: check the current player,
# and increase score of the player who was not hit,
# clear the player who was hit from the screen,
# and set $next_player to the player who was hit
for ((i=0; i < ${#player1_coordinates[@]}; i++))
do
if [[ "${player1_coordinates[${i}]}" == "${x},$((y - 1))" ]]
then
clear_player1
player2_score=$((player2_score + 1))
if ((next_player == 2))
then
switch_player
fi
break 3
elif [[ "${player2_coordinates[${i}]}" == "${x},$((y - 1))" ]]
then
clear_player2
player1_score=$((player1_score + 1))
if ((next_player == 1))
then
switch_player
fi
break 3
fi
done
# Change banana character only if cursor is moved to another place
if ((prev_x != x || prev_y != y))
then
next_banana_frame
fi
# Collision detection END ----------------------------------------------
# Print banana to screen
if ((x >= 0 && x < grid_width && y >= 1 && y <= grid_height))
then
tput cup $(($((top_padding_height + grid_height)) - y)) \
$((left_padding_width + x)) >> ${buffer}
printf "${banana}" >> ${buffer}
refresh_screen
sleep 0.05
fi
# Set previous horizontal ($prev_x) and vertical ($prev_y) coordinates
prev_x=${x}
prev_y=${y}
# Step time
t=$(echo "scale=20; ${t} + 0.005" | bc -l)
done
# If the thrown banana gets out of boundaries
# or the banana hits a building then the next
# player can throw
switch_player
}