laneya2

cave exploration game
git clone https://git.ce9e.org/laneya2.git

commit
5fda4ca01321484765beb80b1d8409e35029d060
parent
5362c5ca5388f3bf9786e57d8d118537a9f715ab
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-09-26 06:50
add health

Diffstat

M game.go 47 ++++++++++++++++++++++++++++-------------------
M server.go 16 +++++++++-------
M static/main.js 10 ++++++++++

3 files changed, 47 insertions, 26 deletions


diff --git a/game.go b/game.go

@@ -11,22 +11,25 @@ import (
   11    11 type Message map[string]interface{}
   12    12 
   13    13 type Player struct {
   14    -1 	Game  *Game
   15    -1 	Send  chan []Message
   16    -1 	conn  *websocket.Conn
   17    -1 	alive bool
   18    -1 	Id    int
   19    -1 	Pos   Point
   20    -1 	Speed float32
   -1    14 	Game        *Game
   -1    15 	Send        chan []Message
   -1    16 	conn        *websocket.Conn
   -1    17 	alive       bool
   -1    18 	Id          int
   -1    19 	Pos         Point
   -1    20 	Speed       float32
   -1    21 	Health      int
   -1    22 	HealthTotal int
   21    23 }
   22    24 
   23    25 type Monster struct {
   24    -1 	Game  *Game
   25    -1 	quit  chan bool
   26    -1 	Id    int
   27    -1 	Rune  rune
   28    -1 	Pos   Point
   29    -1 	Speed float32
   -1    26 	Game   *Game
   -1    27 	quit   chan bool
   -1    28 	Id     int
   -1    29 	Rune   rune
   -1    30 	Pos    Point
   -1    31 	Speed  float32
   -1    32 	Health int
   30    33 }
   31    34 
   32    35 type PlayerMessage struct {
@@ -157,12 +160,13 @@ func (game *Game) generateMap() {
  157   160 			lines = append(lines, makeRect(p2.X, p1.Y, p2.X, p2.Y))
  158   161 
  159   162 			monster := Monster{
  160    -1 				Game:  game,
  161    -1 				quit:  make(chan bool),
  162    -1 				Id:    game.createId(),
  163    -1 				Rune:  'm',
  164    -1 				Pos:   rect.RandomPoint(),
  165    -1 				Speed: 2,
   -1   163 				Game:   game,
   -1   164 				quit:   make(chan bool),
   -1   165 				Id:     game.createId(),
   -1   166 				Rune:   'm',
   -1   167 				Pos:    rect.RandomPoint(),
   -1   168 				Speed:  2,
   -1   169 				Health: 10,
  166   170 			}
  167   171 			game.Monsters[&monster] = true
  168   172 			go monster.run()
@@ -238,6 +242,11 @@ func (game *Game) run() {
  238   242 					"id":     player.Id,
  239   243 				},
  240   244 				Message{
   -1   245 					"action":      "setHealth",
   -1   246 					"health":      player.Health,
   -1   247 					"healthTotal": player.HealthTotal,
   -1   248 				},
   -1   249 				Message{
  241   250 					"action": "setLevel",
  242   251 					"rects":  game.Rects,
  243   252 					"ladder": game.Ladder,

diff --git a/server.go b/server.go

@@ -94,13 +94,15 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
   94    94 	game := getGame(r.PathValue("id"))
   95    95 
   96    96 	player := &Player{
   97    -1 		Game:  game,
   98    -1 		Send:  make(chan []Message),
   99    -1 		conn:  conn,
  100    -1 		alive: true,
  101    -1 		Id:    game.createId(),
  102    -1 		Pos:   Point{0, 0},
  103    -1 		Speed: 20,
   -1    97 		Game:        game,
   -1    98 		Send:        make(chan []Message),
   -1    99 		conn:        conn,
   -1   100 		alive:       true,
   -1   101 		Id:          game.createId(),
   -1   102 		Pos:         Point{0, 0},
   -1   103 		Speed:       20,
   -1   104 		Health:      100,
   -1   105 		HealthTotal: 100,
  104   106 	}
  105   107 	conn.SetPongHandler(func(string) error {
  106   108 		player.alive = true

diff --git a/static/main.js b/static/main.js

@@ -32,6 +32,8 @@ var game = {
   32    32     rects: [],
   33    33     seen: {},
   34    34     objects: {},
   -1    35     health: 1,
   -1    36     healthTotal: 1,
   35    37 
   36    38     getRect(pos, withWalls) {
   37    39         for (const rect of this.rects) {
@@ -170,6 +172,11 @@ var render = function() {
  170   172         }
  171   173     };
  172   174 
   -1   175     var health = Math.round(game.health / game.healthTotal * cols);
   -1   176     commitSpan('='.repeat(health), 1);
   -1   177     commitSpan('='.repeat(cols - health), 0);
   -1   178     $pre.append('\n');
   -1   179 
  173   180     for (let y = 1; y < rows; y++) {
  174   181         var span = '';
  175   182         var spanColor = -1;
@@ -222,6 +229,9 @@ socket.onmessage = function(event) {
  222   229             if (game.objects[msg.id].type === 'player') {
  223   230                 game.updateSeen(msg.pos);
  224   231             }
   -1   232         } else if (msg.action === 'setHealth') {
   -1   233             game.health = msg.health;
   -1   234             game.healthTotal = msg.healthTotal;
  225   235         } else if (msg.action === 'remove') {
  226   236             delete game.objects[msg.id];
  227   237         } else {