laneya2

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

commit
680489b93400fa5fffc8c522d593e39c2b8c846b
parent
aa50b41b740f453883059d53f8bbb66b8504bd3e
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-09-29 09:11
define player stats on server

Diffstat

M game.go 31 ++++++++++++++++++++++++++++---
M monster.go 43 ++++++++++++++++++++++++-------------------
M player.go 19 +++++++++++--------
M server.go 7 +++++--
M static/main.js 26 ++++++++++++++++----------

5 files changed, 84 insertions, 42 deletions


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

@@ -218,9 +218,34 @@ func (game *Game) run() {
  218   218 				"id":     player.Id,
  219   219 			})
  220   220 			player.Enqueue(Message{
  221    -1 				"action":      "setHealth",
  222    -1 				"health":      player.Health,
  223    -1 				"healthTotal": player.HealthTotal,
   -1   221 				"action": "setStat",
   -1   222 				"stat":   "health",
   -1   223 				"value":  player.Health,
   -1   224 			})
   -1   225 			player.Enqueue(Message{
   -1   226 				"action": "setStat",
   -1   227 				"stat":   "healthTotal",
   -1   228 				"value":  player.HealthTotal,
   -1   229 			})
   -1   230 			player.Enqueue(Message{
   -1   231 				"action": "setStat",
   -1   232 				"stat":   "attack",
   -1   233 				"value":  player.Attack,
   -1   234 			})
   -1   235 			player.Enqueue(Message{
   -1   236 				"action": "setStat",
   -1   237 				"stat":   "defense",
   -1   238 				"value":  player.Defense,
   -1   239 			})
   -1   240 			player.Enqueue(Message{
   -1   241 				"action": "setStat",
   -1   242 				"stat":   "lineOfSight",
   -1   243 				"value":  player.LineOfSight,
   -1   244 			})
   -1   245 			player.Enqueue(Message{
   -1   246 				"action": "setStat",
   -1   247 				"stat":   "speed",
   -1   248 				"value":  player.Speed,
  224   249 			})
  225   250 			player.Enqueue(Message{
  226   251 				"action": "setLevel",

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

@@ -3,13 +3,15 @@ package main
    3     3 import "time"
    4     4 
    5     5 type Monster struct {
    6    -1 	Game   *Game
    7    -1 	quit   chan bool
    8    -1 	Id     int
    9    -1 	Rune   rune
   10    -1 	Pos    Point
   11    -1 	Speed  float32
   12    -1 	Health int
   -1     6 	Game    *Game
   -1     7 	quit    chan bool
   -1     8 	Id      int
   -1     9 	Rune    rune
   -1    10 	Pos     Point
   -1    11 	Health  uint
   -1    12 	Attack  uint
   -1    13 	Defense uint
   -1    14 	Speed   uint
   13    15 }
   14    16 
   15    17 type MonsterMessage struct {
@@ -19,20 +21,22 @@ type MonsterMessage struct {
   19    21 
   20    22 func makeMonster(game *Game, pos Point) *Monster {
   21    23 	monster := &Monster{
   22    -1 		Game:   game,
   23    -1 		quit:   make(chan bool),
   24    -1 		Id:     game.createId(),
   25    -1 		Rune:   'm',
   26    -1 		Pos:    pos,
   27    -1 		Speed:  2,
   28    -1 		Health: 10,
   -1    24 		Game:    game,
   -1    25 		quit:    make(chan bool),
   -1    26 		Id:      game.createId(),
   -1    27 		Rune:    'm',
   -1    28 		Pos:     pos,
   -1    29 		Speed:   2,
   -1    30 		Attack:  2,
   -1    31 		Defense: 0,
   -1    32 		Health:  10,
   29    33 	}
   30    34 	go monster.run()
   31    35 	return monster
   32    36 }
   33    37 
   34    38 func (monster *Monster) run() {
   35    -1 	timeout := time.Duration(float32(time.Second) / monster.Speed)
   -1    39 	timeout := time.Duration(float32(time.Second) / float32(monster.Speed))
   36    40 	ticker := time.NewTicker(timeout)
   37    41 	defer ticker.Stop()
   38    42 
@@ -69,9 +73,8 @@ func (monster *Monster) run() {
   69    73 	}
   70    74 }
   71    75 
   72    -1 func (monster *Monster) TakeDamage(amount int) {
   73    -1 	monster.Health -= amount
   74    -1 	if monster.Health <= 0 {
   -1    76 func (monster *Monster) TakeDamage(amount uint) {
   -1    77 	if amount > monster.Health {
   75    78 		monster.quit <- true
   76    79 		delete(monster.Game.Monsters, monster)
   77    80 		monster.Game.addToPile(monster.Pos, "potion", 1)
@@ -79,6 +82,8 @@ func (monster *Monster) TakeDamage(amount int) {
   79    82 			"action": "remove",
   80    83 			"id":     monster.Id,
   81    84 		})
   -1    85 	} else {
   -1    86 		monster.Health -= amount
   82    87 	}
   83    88 }
   84    89 
@@ -87,7 +92,7 @@ func (monster *Monster) Move(dir string) {
   87    92 	pos := monster.Pos.Move(dir)
   88    93 	player := game.getPlayerAt(pos)
   89    94 	if player != nil {
   90    -1 		player.TakeDamage(2)
   -1    95 		player.TakeDamage(monster.Attack)
   91    96 	} else if game.getMonsterAt(pos) == nil && game.IsFree(pos) {
   92    97 		monster.Pos = pos
   93    98 		game.Enqueue(Message{

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

@@ -11,9 +11,12 @@ type Player struct {
   11    11 	alive       bool
   12    12 	Id          int
   13    13 	Pos         Point
   14    -1 	Speed       float32
   15    14 	Health      uint
   16    15 	HealthTotal uint
   -1    16 	Attack      uint
   -1    17 	Defense     uint
   -1    18 	LineOfSight uint
   -1    19 	Speed       uint
   17    20 	Inventory   map[string]uint
   18    21 }
   19    22 
@@ -39,9 +42,9 @@ func (player *Player) TakeDamage(amount uint) {
   39    42 	} else {
   40    43 		player.Health -= amount
   41    44 		player.Enqueue(Message{
   42    -1 			"action":      "setHealth",
   43    -1 			"health":      player.Health,
   44    -1 			"healthTotal": player.HealthTotal,
   -1    45 			"action": "setStat",
   -1    46 			"stat":   "health",
   -1    47 			"value":  player.Health,
   45    48 		})
   46    49 	}
   47    50 }
@@ -52,9 +55,9 @@ func (player *Player) Heal(amount uint) {
   52    55 		player.Health = player.HealthTotal
   53    56 	}
   54    57 	player.Enqueue(Message{
   55    -1 		"action":      "setHealth",
   56    -1 		"health":      player.Health,
   57    -1 		"healthTotal": player.HealthTotal,
   -1    58 		"action": "setStat",
   -1    59 		"stat":   "health",
   -1    60 		"value":  player.Health,
   58    61 	})
   59    62 }
   60    63 
@@ -102,7 +105,7 @@ func (player *Player) Move(dir string) {
  102   105 	pos := player.Pos.Move(dir)
  103   106 	monster := game.getMonsterAt(pos)
  104   107 	if monster != nil {
  105    -1 		monster.TakeDamage(5)
   -1   108 		monster.TakeDamage(player.Attack)
  106   109 	} else if game.IsFree(pos) {
  107   110 		player.Pos = pos
  108   111 		game.Enqueue(Message{

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

@@ -42,7 +42,7 @@ func (player *Player) readPump() {
   42    42 		if timer != nil {
   43    43 			timer.Stop()
   44    44 		}
   45    -1 		timeout := time.Duration(float32(time.Second) / player.Speed)
   -1    45 		timeout := time.Duration(float32(time.Second) / float32(player.Speed))
   46    46 		timer = time.AfterFunc(time.Until(lastTime.Add(timeout)), func() {
   47    47 			lastTime = time.Now()
   48    48 			player.Game.Msg <- PlayerMessage{player, msg}
@@ -104,9 +104,12 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
  104   104 		alive:       true,
  105   105 		Id:          game.createId(),
  106   106 		Pos:         Point{0, 0},
  107    -1 		Speed:       20,
  108   107 		Health:      100,
  109   108 		HealthTotal: 100,
   -1   109 		Attack:      5,
   -1   110 		Defense:     0,
   -1   111 		LineOfSight: 5,
   -1   112 		Speed:       20,
  110   113 		Inventory:   make(map[string]uint),
  111   114 	}
  112   115 	conn.SetPongHandler(func(string) error {

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

@@ -1,7 +1,6 @@
    1     1 import onDPad from './dpad.js';
    2     2 
    3     3 var $pre = document.querySelector('pre');
    4    -1 var radius = 5;
    5     4 
    6     5 var params = new URLSearchParams(location.search);
    7     6 var gameId = params.get('game');
@@ -51,8 +50,14 @@ var game = {
   51    50     rects: [],
   52    51     seen: {},
   53    52     objects: {},
   54    -1     health: 1,
   55    -1     healthTotal: 1,
   -1    53     stats: {
   -1    54         health: 1,
   -1    55         healthTotal: 1,
   -1    56         attack: 0,
   -1    57         defense: 0,
   -1    58         speed: 0,
   -1    59         lineOfSight: 0,
   -1    60     },
   56    61     inventory: {},
   57    62 
   58    63     getRect(pos, withWalls) {
@@ -67,7 +72,8 @@ var game = {
   67    72         // check radius
   68    73         var dx = a.x - b.x;
   69    74         var dy = a.y - b.y;
   70    -1         if (dx * dx + dy * dy >= radius * radius) {
   -1    75         var r = this.stats.lineOfSight;
   -1    76         if (dx * dx + dy * dy >= r * r) {
   71    77             return false;
   72    78         }
   73    79 
@@ -147,9 +153,10 @@ var game = {
  147   153     },
  148   154 
  149   155     updateSeen(pos) {
  150    -1         for (let dy = -radius; dy <= radius; dy++) {
   -1   156         var r = this.stats.lineOfSight;
   -1   157         for (let dy = -r; dy <= r; dy++) {
  151   158             const y = pos.y + dy;
  152    -1             for (let dx = -radius; dx <= radius; dx++) {
   -1   159             for (let dx = -r; dx <= r; dx++) {
  153   160                 const x = pos.x + dx;
  154   161                 if (!this.seen[[x, y]] && this.inView(pos, {x, y})) {
  155   162                     this.seen[[x, y]] = true;
@@ -202,7 +209,7 @@ var screen = {
  202   209     },
  203   210 
  204   211     renderHealth() {
  205    -1         var health = Math.round(game.health / game.healthTotal * this.cols);
   -1   212         var health = Math.round(game.stats.health / game.stats.healthTotal * this.cols);
  206   213         this.commitSpan('='.repeat(health), 1);
  207   214         this.commitSpan('='.repeat(this.cols - health), 0);
  208   215         $pre.append('\n');
@@ -313,9 +320,8 @@ socket.onmessage = function(event) {
  313   320             if (game.objects[msg.id].type === 'player') {
  314   321                 game.updateSeen(msg.pos);
  315   322             }
  316    -1         } else if (msg.action === 'setHealth') {
  317    -1             game.health = msg.health;
  318    -1             game.healthTotal = msg.healthTotal;
   -1   323         } else if (msg.action === 'setStat') {
   -1   324             game.stats[msg.stat] = msg.value;
  319   325         } else if (msg.action === 'remove') {
  320   326             delete game.objects[msg.id];
  321   327         } else if (msg.action === 'setInventory') {