laneya2

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

commit
dd5a761d22c2f2d3ac629e615e497cf55e29e6ca
parent
54782199626931095c38e35030f883193e7bcae7
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-10-10 06:03
declerative monster definition

Diffstat

M monster.go 104 +++++++++++++++++++++++++++++++++++++++++++++++--------------

1 files changed, 80 insertions, 24 deletions


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

@@ -6,6 +6,18 @@ import (
    6     6 	"time"
    7     7 )
    8     8 
   -1     9 type MonsterClass struct {
   -1    10 	Rune          rune
   -1    11 	HealthBase    float64
   -1    12 	HealthFactor  float64
   -1    13 	AttackBase    float64
   -1    14 	AttackFactor  float64
   -1    15 	DefenseBase   float64
   -1    16 	DefenseFactor float64
   -1    17 	Speed         int
   -1    18 	Probability   float64
   -1    19 }
   -1    20 
    9    21 type Monster struct {
   10    22 	Game    *Game
   11    23 	quit    chan bool
@@ -19,37 +31,81 @@ type Monster struct {
   19    31 	Speed   int
   20    32 }
   21    33 
   -1    34 var MonsterClasses = []MonsterClass{
   -1    35 	MonsterClass{
   -1    36 		Rune:         'm',
   -1    37 		HealthBase:   10,
   -1    38 		HealthFactor: 1,
   -1    39 		AttackBase:   2,
   -1    40 		AttackFactor: 1,
   -1    41 		DefenseBase:  2,
   -1    42 		Probability:  5,
   -1    43 	},
   -1    44 	MonsterClass{
   -1    45 		Rune:         'M',
   -1    46 		HealthBase:   20,
   -1    47 		HealthFactor: 2,
   -1    48 		AttackBase:   8,
   -1    49 		AttackFactor: 1,
   -1    50 		DefenseBase:  8,
   -1    51 		Speed:        -2,
   -1    52 		Probability:  1,
   -1    53 	},
   -1    54 	MonsterClass{
   -1    55 		Rune:         's',
   -1    56 		HealthBase:   5,
   -1    57 		HealthFactor: 0.5,
   -1    58 		AttackBase:   2,
   -1    59 		AttackFactor: 1,
   -1    60 		DefenseBase:  2,
   -1    61 		Speed:        10,
   -1    62 		Probability:  2,
   -1    63 	},
   -1    64 	MonsterClass{
   -1    65 		Rune:         'z',
   -1    66 		HealthBase:   12,
   -1    67 		HealthFactor: 1.2,
   -1    68 		AttackBase:   4,
   -1    69 		AttackFactor: 1,
   -1    70 		DefenseBase:  4,
   -1    71 		Speed:        -5,
   -1    72 		Probability:  2,
   -1    73 	},
   -1    74 }
   -1    75 
   -1    76 func randomMonsterClass() *MonsterClass {
   -1    77 	total := 0.0
   -1    78 	for _, c := range MonsterClasses {
   -1    79 		total += c.Probability
   -1    80 	}
   -1    81 
   -1    82 	x := rand.Float64()
   -1    83 	for _, c := range MonsterClasses {
   -1    84 		p := c.Probability / total
   -1    85 		if x < p {
   -1    86 			return &c
   -1    87 		} else {
   -1    88 			x -= p
   -1    89 		}
   -1    90 	}
   -1    91 	return &MonsterClasses[0]
   -1    92 }
   -1    93 
   22    94 func makeMonster(game *Game, pos Point) *Monster {
   -1    95 	f := float64(game.Level)
   -1    96 	c := randomMonsterClass()
   -1    97 
   23    98 	monster := &Monster{
   24    99 		Game:    game,
   25   100 		quit:    make(chan bool),
   26   101 		Id:      game.createId(),
   27    -1 		Rune:    'm',
   -1   102 		Rune:    c.Rune,
   28   103 		Pos:     pos,
   29   104 		Dir:     "right",
   30    -1 		Speed:   0,
   31    -1 		Attack:  2 + float64(game.Level),
   32    -1 		Defense: 2,
   33    -1 		Health:  10 + float64(game.Level),
   34    -1 	}
   35    -1 
   36    -1 	r := rand.Intn(10)
   37    -1 	if r == 0 {
   38    -1 		monster.Rune = 'M'
   39    -1 		monster.Attack += 6
   40    -1 		monster.Defense += 6
   41    -1 		monster.Health *= 2
   42    -1 		monster.Speed -= 2
   43    -1 	} else if r < 3 {
   44    -1 		monster.Rune = 's'
   45    -1 		monster.Speed += 10
   46    -1 		monster.Health /= 2
   47    -1 	} else if r < 5 {
   48    -1 		monster.Rune = 'z'
   49    -1 		monster.Attack += 2
   50    -1 		monster.Defense += 2
   51    -1 		monster.Health *= 1.2
   52    -1 		monster.Speed -= 5
   -1   105 		Health:  c.HealthBase + c.HealthFactor*f,
   -1   106 		Attack:  c.AttackBase + c.AttackFactor*f,
   -1   107 		Defense: c.DefenseBase + c.DefenseFactor*f,
   -1   108 		Speed:   c.Speed,
   53   109 	}
   54   110 
   55   111 	go monster.run()