parent
ae34b07e21
commit
cf2a2d4d84
@ -0,0 +1,48 @@
|
|||||||
|
/* Copyright 2018-2020 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of
|
||||||
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
|
||||||
|
* applicable law or agreed to in writing, software distributed under the
|
||||||
|
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
|
||||||
|
* OF ANY KIND, either express or implied. See the License for the specific
|
||||||
|
* language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.github.drinkjava2.frog.objects;
|
||||||
|
|
||||||
|
import static com.github.drinkjava2.frog.Env.ENV_HEIGHT;
|
||||||
|
import static com.github.drinkjava2.frog.Env.ENV_WIDTH;
|
||||||
|
import static com.github.drinkjava2.frog.Env.FOOD_QTY;
|
||||||
|
import static com.github.drinkjava2.frog.Env.bricks;
|
||||||
|
|
||||||
|
import com.github.drinkjava2.frog.util.RandomUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Food randomly scatter on Env
|
||||||
|
*
|
||||||
|
* @author Yong Zhu
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
|
public class Food implements Object {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build() {
|
||||||
|
for (int i = 0; i < FOOD_QTY; i++) // 生成食物
|
||||||
|
bricks[RandomUtils.nextInt(ENV_WIDTH)][RandomUtils.nextInt(ENV_HEIGHT)] = Material.FOOD;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destory() {
|
||||||
|
for (int i = 0; i < ENV_WIDTH; i++) {// 清除食物
|
||||||
|
for (int j = 0; j < ENV_HEIGHT; j++)
|
||||||
|
if (bricks[i][j] == Material.FOOD)
|
||||||
|
bricks[i][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void active(int screen) {
|
||||||
|
// Food do not have any active
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
/* Copyright 2018-2020 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of
|
||||||
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
|
||||||
|
* applicable law or agreed to in writing, software distributed under the
|
||||||
|
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
|
||||||
|
* OF ANY KIND, either express or implied. See the License for the specific
|
||||||
|
* language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.github.drinkjava2.frog.objects;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object means some thing in Env
|
||||||
|
*
|
||||||
|
* @author Yong Zhu
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
|
public class Material {
|
||||||
|
public static final byte VISIBLE = 10; // if>=10 will visible to frog
|
||||||
|
public static final byte KILLFROG = 20; // if>=20 will kill frog
|
||||||
|
|
||||||
|
public static final byte NO = 0;
|
||||||
|
public static final byte SEESAW_BASE = 1; // 1~9 is invisible to frog
|
||||||
|
|
||||||
|
public static final byte FOOD = VISIBLE + 1;
|
||||||
|
public static final byte SEESAW = VISIBLE + 2; // if <0 will not cause frog die
|
||||||
|
|
||||||
|
public static final byte BRICK = KILLFROG + 1;
|
||||||
|
public static final byte TRAP = KILLFROG + 2;
|
||||||
|
|
||||||
|
public static Color color(byte material) {
|
||||||
|
if (material == TRAP)
|
||||||
|
return Color.LIGHT_GRAY;
|
||||||
|
else
|
||||||
|
return Color.BLACK;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
/* Copyright 2018-2020 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of
|
||||||
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
|
||||||
|
* applicable law or agreed to in writing, software distributed under the
|
||||||
|
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
|
||||||
|
* OF ANY KIND, either express or implied. See the License for the specific
|
||||||
|
* language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.github.drinkjava2.frog.objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object means some thing in Env
|
||||||
|
*
|
||||||
|
* @author Yong Zhu
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
|
public interface Object {
|
||||||
|
|
||||||
|
public void build();
|
||||||
|
|
||||||
|
public void destory();
|
||||||
|
|
||||||
|
public void active(int screen);
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
/* Copyright 2018-2020 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of
|
||||||
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
|
||||||
|
* applicable law or agreed to in writing, software distributed under the
|
||||||
|
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
|
||||||
|
* OF ANY KIND, either express or implied. See the License for the specific
|
||||||
|
* language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.github.drinkjava2.frog.objects;
|
||||||
|
|
||||||
|
import static com.github.drinkjava2.frog.Env.ENV_HEIGHT;
|
||||||
|
import static com.github.drinkjava2.frog.Env.ENV_WIDTH;
|
||||||
|
import static com.github.drinkjava2.frog.Env.bricks;
|
||||||
|
|
||||||
|
import com.github.drinkjava2.frog.Env;
|
||||||
|
import com.github.drinkjava2.frog.Frog;
|
||||||
|
import com.github.drinkjava2.frog.util.RandomUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a seesaw to train frog's balance
|
||||||
|
*
|
||||||
|
* @author Yong Zhu
|
||||||
|
* @since 2.0.1
|
||||||
|
*/
|
||||||
|
public class SeeSaw implements Object {
|
||||||
|
private static final int LEGNTH = 300;
|
||||||
|
private static final int CENTER_X = Env.ENV_WIDTH / 2;
|
||||||
|
private static final int CENTER_Y = Env.ENV_HEIGHT / 2;
|
||||||
|
|
||||||
|
private double angle = 0;// -PI/4 to PI/4
|
||||||
|
private double leftWeight = 0;
|
||||||
|
private double rightWeight = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build() {
|
||||||
|
angle = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destory() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void active(int screen) {
|
||||||
|
for (int i = 0; i < ENV_WIDTH; i++) {// 清除食物
|
||||||
|
for (int j = 0; j < ENV_HEIGHT; j++)
|
||||||
|
if (bricks[i][j] == Material.SEESAW)
|
||||||
|
bricks[i][j] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (RandomUtils.percent(2))
|
||||||
|
leftWeight = RandomUtils.nextFloat() * 3;
|
||||||
|
if (RandomUtils.percent(2))
|
||||||
|
rightWeight = RandomUtils.nextFloat() * 3;
|
||||||
|
Frog f = Env.frogs.get(screen);
|
||||||
|
|
||||||
|
if (f.x < (CENTER_X - LEGNTH / 2) || f.x > (CENTER_X + LEGNTH / 2))
|
||||||
|
f.energy -= 100000; // 如果走出跷跷板外则扣分,出局
|
||||||
|
double left = leftWeight - (f.x - CENTER_X);
|
||||||
|
double right = rightWeight + (f.x - CENTER_X);
|
||||||
|
// right - left need in -100 to +100
|
||||||
|
angle = angle + (right - left) * Math.PI * .000001;
|
||||||
|
if (angle > Math.PI / 6) {
|
||||||
|
angle = Math.PI / 6;
|
||||||
|
f.energy -= 200;
|
||||||
|
}
|
||||||
|
if (angle < -Math.PI / 6) {
|
||||||
|
angle = -Math.PI / 6;
|
||||||
|
f.energy -= 200;
|
||||||
|
}
|
||||||
|
f.y = CENTER_Y + (int) Math.round((f.x - CENTER_X) * Math.tan(angle));
|
||||||
|
f.energy -= Math.abs(angle) * 180; // 角度越大,扣分越多
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
for (int l = -LEGNTH / 2; l <= LEGNTH / 2; l++) {
|
||||||
|
x = (int) Math.round(l * Math.cos(angle));
|
||||||
|
y = (int) Math.round(l * Math.sin(angle));
|
||||||
|
Env.bricks[CENTER_X + x][CENTER_Y + y] = Material.SEESAW;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 画底座
|
||||||
|
for (int i = 1; i < 10; i++) {
|
||||||
|
Env.bricks[CENTER_X - i][CENTER_Y + i] = Material.SEESAW_BASE;
|
||||||
|
Env.bricks[CENTER_X + i][CENTER_Y + i] = Material.SEESAW_BASE;
|
||||||
|
}
|
||||||
|
for (int i = -10; i < 10; i++)
|
||||||
|
Env.bricks[CENTER_X + i][CENTER_Y + 10] = Material.SEESAW_BASE;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
/* Copyright 2018-2020 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
|
* use this file except in compliance with the License. You may obtain a copy of
|
||||||
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
|
||||||
|
* applicable law or agreed to in writing, software distributed under the
|
||||||
|
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
|
||||||
|
* OF ANY KIND, either express or implied. See the License for the specific
|
||||||
|
* language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.github.drinkjava2.frog.objects;
|
||||||
|
|
||||||
|
import static com.github.drinkjava2.frog.Env.ENV_HEIGHT;
|
||||||
|
import static com.github.drinkjava2.frog.Env.ENV_WIDTH;
|
||||||
|
import static com.github.drinkjava2.frog.Env.bricks;
|
||||||
|
|
||||||
|
import com.github.drinkjava2.frog.Frog;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trap will kill all frogs inside of it, if frog's position has material and
|
||||||
|
* it's not food, frog will die
|
||||||
|
*
|
||||||
|
* @author Yong Zhu
|
||||||
|
* @since 2019-08-05
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class Trap implements Object {
|
||||||
|
private static final int X1 = ENV_WIDTH / 2 - 350 / 2; // 陷阱左上角
|
||||||
|
private static final int Y1 = ENV_HEIGHT / 2 - 20 / 2; // 陷阱左上角
|
||||||
|
private static final int X2 = ENV_WIDTH / 2 + 350 / 2; // 陷阱右下角
|
||||||
|
private static final int Y2 = ENV_HEIGHT / 2 + 20 / 2; // 陷阱右下角
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build() {
|
||||||
|
for (int x = X1; x <= X2; x++)
|
||||||
|
for (int y = Y1; y <= Y2; y++)
|
||||||
|
bricks[x][y] = Material.TRAP;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destory() {
|
||||||
|
for (int x = X1; x <= X2; x++)
|
||||||
|
for (int y = Y1; y <= Y2; y++)
|
||||||
|
bricks[x][y] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void active(int screen) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean inTrap(Frog f) {
|
||||||
|
return f.x >= X1 && f.x <= X2 && f.y >= Y1 && f.y <= Y2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
After Width: | Height: | Size: 304 KiB |
Loading…
Reference in new issue