Skip to content

Commit

Permalink
アイテムの処理を追加。武器が持てるように
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Feb 19, 2020
1 parent 6404c0e commit 8d1fd30
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 6 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ MAKE = make
ENCODING = UTF8

build :
make clean
# make clean
make game/Random.class
make game/item/Item.class
make game/item/weapon/Weapon.class
make game/item/weapon/None.class
make game/item/weapon/sword/Sword.class
make game/character/player/Player.class
make game/ConsEsc.class
make game/Console.class
make game/Game.class

run :
make build
# make build
$(JAVA) game.Game

javadoc :
Expand Down
26 changes: 22 additions & 4 deletions game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;

import game.ConsEsc;
import game.character.Status;
import game.character.player.Player;

import static game.Console.*;
Expand Down Expand Up @@ -46,8 +47,7 @@ public static void main(String[] args)

private static void maintown()
{
write("---------------");
write("初めの町 住宅街");
write("---初めの町 住宅街---");
putll(loginedPlayer.getName() + "はどうしようか?");
write("1.冒険に外へ行く");
write("2.市場に散歩に行く");
Expand All @@ -62,10 +62,24 @@ private static void maintown()
/* アイテムの購入やイベントの発生など */
write("しかし自分はお金を持っていなかった。");
write("だからどこにも行かず戻ってきた。");
game.item.weapon.sword.Sword s = new game.item.weapon.sword.Sword();
s.use(loginedPlayer);
}else if(c == '3'){
Status st = loginedPlayer.getStatus();
/* 自分自身を見つめる */
/* ステータスの表示 */
putfn("%s lv.xxx", loginedPlayer.getName());
putfn("%s lv.%d", loginedPlayer.getName(), st.lv);
putfn("HP:%d/%d", st.hp, st.mhp);
putfn("AP:%d", st.ap);
putfn("BP:%d", st.bp);
putfn("SP:%d", st.sp);
putfn("MAP:%d", st.map);
putfn("MBP:%d", st.mbp);
putfn("MP:%d/%d", st.mp, st.mmp);
putfn("money:$%8d", loginedPlayer.getMoney());
write("");
/* もし武器をつけてないなら"つけてないよ"と表示 */
putfn("weapon:%s", loginedPlayer.getWeapon() == null ? "つけてないよ" : loginedPlayer.getWeapon().getName());
}else if(c == 'x'){
/* ゲーム終了 */
write("お疲れ様!ゲームを終了します!");
Expand Down Expand Up @@ -146,7 +160,11 @@ public static String read()
{
try{
String s = getInputString();
return s;
if(s.length() > 0) return s;
else{
error("文字が入力されていません。");
return read();
}
}catch(IOException ie){
error("入出力でエラーが発生しました!");
return null;
Expand Down
13 changes: 13 additions & 0 deletions game/Random.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,17 @@ public static long rand(long limit)
r = r.mod(BigInteger.valueOf(limit));
return r.longValue();
}

/**
* 1から100までの乱数を生成します。
*
* @return 1から100までの乱数の値
*/
public static long _rand()
{
long val = rand(1000000);
/* 0から99までになる */
double d = Math.ceil(val / 10000);
return (long)d + 1;
}
}
122 changes: 122 additions & 0 deletions game/character/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package game.character;

/**
* キャラクターの体力や魔力などを管理するクラスです。
*/
public class Status implements Cloneable
{
/**
* レベル
*/
public long lv;
/**
* 最大体力
*/
public long mhp;
/**
* 現在体力
*/
public long hp;
/**
* 攻撃力
*/
public long ap;
/**
* 防御力
*/
public long bp;
/**
* 素早さ
*/
public long sp;
/**
* 魔法攻撃力
*/
public long map;
/**
* 魔法防御力
*/
public long mbp;
/**
* 最大魔力
*/
public long mmp;
/**
* 現在魔力
*/
public long mp;
/**
* 回避率
*/
public long avoid;
/**
* 逃げられる確率
*/
public long flee;

/**
* ステータスの初期化をします。
* 設定される値は適当です。
*/
public Status()
{
lv = 1;
mhp = 0;
hp = mhp;
ap = bp = sp = 0;
map = mbp = 0;
mmp = 0;
mp = mmp;
avoid = flee = 0;
}

/**
* ステータス同士を足し合わせます。
*
* @param st 足し合わせられるステータス
*/
public void add(Status st)
{
mhp += st.mhp;
hp += st.mhp;
ap += st.ap;
bp += st.bp;
sp += st.sp;
map += st.map;
mbp += st.mbp;
mmp += st.mmp;
mp += st.mmp;
avoid += st.avoid;
flee += st.flee;
}

/**
* ステータスを減らします。
*
* @param st 減らすステータスを示す
*/
public void sub(Status st)
{
mhp -= st.mhp;
hp -= st.hp;
ap -= st.ap;
bp -= st.bp;
sp -= st.sp;
map -= st.map;
mbp -= st.mbp;
mmp -= st.mmp;
mp -= st.mp;
avoid -= st.avoid;
flee -= st.flee;
}

@Override public Status clone()
{
try{
return (Status)super.clone();
}catch(CloneNotSupportedException cnse){
/* 絶対に発生しないはず */
throw new InternalError();
}
}
}
105 changes: 105 additions & 0 deletions game/character/player/Player.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
package game.character.player;

import game.character.Status;
import game.item.weapon.Weapon;
import game.item.weapon.None;

/**
* プレイヤーの分身として、ゲーム内で活動する
* キャラクターのクラスです。
*
* @version 0.0.1p
*/
public class Player
{
/* プレイヤーの名前 */
private String name;

/* プレイヤーの能力 */
private Status status;

/* 現在のお金 */
private long money;

/* プレイヤーの装備している武器 装備していない場合null */
private Weapon weapon = new None();

/* ステータスの初期化 */
private void statusInit()
{
status = new Status();
status.mhp = 10;
status.hp = status.mhp;
status.ap = status.bp = status.sp = 2;
status.map = status.mbp = 2;
status.mmp = 1;
status.mp = status.mmp;
}

/**
* 新しいPlayerオブジェクトを生成します。
*
Expand All @@ -17,6 +44,9 @@ public class Player
public Player(String name)
{
this.name = name;

money = 0;
statusInit();
}

/**
Expand All @@ -28,4 +58,79 @@ public String getName()
{
return name;
}

/**
* プレイヤーの所持金を返します。
*
* @return 所持金
*/
public long getMoney()
{
return money;
}

/**
* プレイヤーの所持金を追加します。
*
* @param m 追加させる金額
*/
public void addMoney(long m)
{
money += m;
}

/**
* プレイヤーの所持金を減らします。
*
* @param m 減少させる金額
*/
public void subMOney(long m)
{
money -= m;
}

/**
* プレイヤーの能力を取得します。
*
* @return プレイヤーの現在のステータス
*/
public Status getStatus()
{
return status;
}

/**
* 現在装備している武器を返します。
*
* @return 現在装備している武器 装備していない場合null
*/
public Weapon getWeapon()
{
return weapon;
}

/**
* プレイヤーの武器をセットします。
* 自動で武器を外すことはありません。
*
* @param w 装備する武器 nullの場合無視される
*/
public void addWeapon(Weapon w)
{
if(w != null){
status.add(w.getStatus());
weapon = w;
}
}

/**
* プレイヤーから武器を外します。
*/
public void removeWeapon()
{
if(weapon != null && !(weapon instanceof None)){
status.sub(weapon.getStatus());
weapon = new None();
}
}
}
Loading

0 comments on commit 8d1fd30

Please sign in to comment.